|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Calculating a checksumWORD CalcTitleChecksum( char* pszTitle, // A pointer to the name of the savegame int cb) // set to either 39 or 47 for TTO or TTD savegames { WORD sum = 0; for(int i=0; i<cb; i++) { sum += pszTitle[i]; sum = (sum << 1) + (sum >> 15); } return sum ^ 0xAAAA; } It comes from http://www.ttdpatch.net/chris_becke_ttdlx.html#hack I have tried Dim lCalculatedChecksum As Long Dim lChecksum As Long Dim Data() As Byte Dim tl As New CTwiddleLong For i = 0 To 46 lCalculatedChecksum = lCalculatedChecksum + Data(i) '+ Data(i + 1) tl.Value = lCalculatedChecksum lCalculatedChecksum = tl.RotateL Next lCalculatedChecksum = (lCalculatedChecksum Xor &HAAAA) Data() is being filled from a file. CTwiddleLong comes from Karl's Twiddle sample. -- ClassicVB Users Regroup! comp.lang.basic.visual.misc Free usenet access at http://www.eternal-september.org Leo wrote:
Show quoteHide quote > Hi all can someone please help me convert this C sample into VB6 You'll have problems with that because you're rotating a long and not> please WORD CalcTitleChecksum( > char* pszTitle, // A pointer to the name of the savegame > int cb) // set to either 39 or 47 for TTO or TTD > savegames { > WORD sum = 0; > > for(int i=0; i<cb; i++) > { > sum += pszTitle[i]; > sum = (sum << 1) + (sum >> 15); > } > > return sum ^ 0xAAAA; > } > > It comes from http://www.ttdpatch.net/chris_becke_ttdlx.html#hack > > I have tried > Dim lCalculatedChecksum As Long > Dim lChecksum As Long > Dim Data() As Byte > Dim tl As New CTwiddleLong > > For i = 0 To 46 > lCalculatedChecksum = lCalculatedChecksum + Data(i) '+ Data(i > + 1) > tl.Value = lCalculatedChecksum > lCalculatedChecksum = tl.RotateL > Next > > lCalculatedChecksum = (lCalculatedChecksum Xor &HAAAA) > > Data() is being filled from a file. > CTwiddleLong comes from Karl's Twiddle sample. a short, and because VB doesn't offer an unsigned WORD data type. All your rotates will leave a zero in the lowest bit, and eventually you'll wind up with a zero result by shifting all those 0 bits left 47 times. Using VB alone I can see at least one (naive) solution -- I'm sure there are cleverer ones: Dim Sum As Long ' the chinese variable Dim Idx As Long For Idx = 0 to 46 Sum = (Sum + Data(Idx)) * 2 ' ' * 2 is like a left shift << 1 ' Sum = Sum - CLng(CBool(Sum And 65536)) And 65535 ' ' equivalent to: ' If Sum And &h10000 Then ' Sum = Sum + 1 ' End If ' Sum = Sum And &HFFFF& Next Sum = Sum Xor &HAAAA& Hi Jim,
Actually, I think that's about as good as it gets in VB6 (VB.NET has bit-shift operators... No, I'm not saying one should go that route because of that). Dick -- Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net Author of Visual Basic Programmer's Guide to Serial Communications, 4th Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July 2006. "DickGrier" <dick_grierNOSPAM@msn.com> wrote in message So does PowerBasic, and so do lots of other programming languages and, news:OAzKFMiRLHA.2100@TK2MSFTNGP04.phx.gbl... > Actually, I think that's about as good as it gets > in VB6 (VB.NET has bit-shift operators... unlike VB.NOT, PowerBasic is not bloatware. Mike |
|||||||||||||||||||||||