C++ to VB help
I wrote a C++ DOS app (long ago) to do some checksuming for me. I've spent about 3 hours trying to get this function converted into Visual Basic, but I just can't get it working.
void main(int argc, char *argv[])
{
unsigned int check=0xA55A;
unsigned int sum=0;
unsigned int ochar;
int i=0;
for (i=0; i<(argc-1); i++) {
sscanf(argv[i+1], "%x", &ochar);
sum += ochar ^ (i&0xFF);
sum &= 0xFFFF;
check += sum;
check = ((check >> 1) & 0x7FFF) | ((check & 1) << 15);
}
printf("Checksum: %04X\n", check);
}
Test data: (show in hex because of control chars)
+----------+-------------------------------------------------+
| checksum | data |
+----------+-------------------------------------------------+
| 5A D1 | 35 35 00 35 |
+----------+-------------------------------------------------+
| 5A BA | 35 35 00 0B |
+----------+-------------------------------------------------+
| E9 1D | 35 35 00 35 01 00 00 05 00 17 02 41 1F |
+----------+-------------------------------------------------+
| A9 6D | 35 35 00 35 01 00 00 2F 00 0C 00 2B 01 05 E0 FF |
| | 10 08 00 00 1F 2C 08 48 65 6C 6C 6F 0C 1F 20 0E |
| | 1F 41 09 02 05 E8 70 10 00 10 28 57 6F 72 6C 64 |
| | 0C 1F 20 01 1C 4E 12 |
+----------+-------------------------------------------------+
Mark Hensler
If there is no answer on Google, then there is no question.
Greg K posted this at 16:57 — 18th February 2004.
He has: 2,145 posts
Joined: Nov 2003
I know VB pretty well and could probably help you if you reposted the code and added in some comments as to what it is actually doing in C, as I'm not good with C.
-Greg
Mark Hensler posted this at 18:13 — 18th February 2004.
He has: 4,048 posts
Joined: Aug 2000
This is what I have so far. Neither method is producing the correct results (but both the same wrong result at least). I think it's getting close, but it's hard to tell.
I'm questioning this line of code:
check = ((check \ 2) And &H7FFF) Or ((check And 1) * &H8000)
Using a test string "55", this VB function returns hex E999. The correct checksum is hex 6998. The difference being hex 8001... the first and last bit.
Public Function ChecksumString(ByVal strData As String)
Dim check As Integer
Dim sum As Integer
Dim i As Byte
Dim chars() As Byte
Dim char As Byte
'just for use while designing function
Dim method
method = 1
'base value for Check
check = &HA55A
sum = 0
chars = StrConv(strData, vbFromUnicode)
If method = 1 Then
For i = 0 To UBound(chars)
char = chars(i)
MsgBox (Chr(char) & " " & Hex(char))
sum = sum + (char Xor (i And &HFF))
sum = sum And &HFFFF
check = check + sum
check = ((check \ 2) And &H7FFF) Or ((check And 1) * &H8000)
Next
Else
For i = 0 To Len(strData) - 1
char = CByte(Asc(Mid(strData, i + 1, 1)))
MsgBox (Chr(char) & " " & Hex(char))
sum = sum + (char Xor (i And &HFF))
sum = sum And &HFFFF
check = check + sum
check = ((check \ 2) And &H7FFF) Or ((check And 1) * &H8000)
Next
End If
ChecksumString = check
End Function
Mark Hensler
If there is no answer on Google, then there is no question.
Mark Hensler posted this at 07:37 — 19th February 2004.
He has: 4,048 posts
Joined: Aug 2000
I've just finished a few tests, and I believe I have a working function now. Here's the code:
Public Function ChecksumString(ByVal strData As String)
Dim check As Integer
Dim sum As Integer
Dim i As Byte
Dim chars() As Byte
Dim char As Byte
'base value for Check
check = &HA55A
sum = 0
chars = StrConv(strData, vbFromUnicode)
For i = 0 To UBound(chars)
char = chars(i)
sum = sum + (char Xor (i And &HFF))
sum = sum And &HFFFF
check = check + sum
check = (((check And &HFFFE) \ 2) And &H7FFF) Or ((check And 1) * &H8000)
Next
ChecksumString = check
End Function
The last hurdle was the fact that check was a signed integer, and the integer division is an arithmatic shift right rather than a logic shift. (I haven't found a way to make unsigned integers in VB.)
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.