I'm having a hell of a time figure out how to translate this piece of code.
Public Function AdDDNc32(ByVal Item As String, ByVal Crc32 As Long) As Long
'Declare following variables
Dim bCharValue As Byte, iCounter As Integer, lIndex As Long
Dim lAccValue As Long, lTableValue As Long
On Error Resume Next
'Iterate through the string that is to be checksum-computed
For iCounter = 1 To Len(Item)
'Selects the current character and converts it to an ASCII value
bCharValue = Asc(Mid$(Item, iCounter, 1))
'Only update CRC for valid ASCII characters, 32 to 126
If (bCharValue > 31) And (bCharValue < 127) Then
'Right shift an Unsigned Long 8 bits
lAccValue = Crc32 And &HFFFFFF00
lAccValue = lAccValue \ &H100
lAccValue = lAccValue And &HFFFFFF
'Now select the right adding value from the holding table
lIndex = Crc32 And &HFF
lIndex = lIndex Xor bCharValue 'inverts the byte
lTableValue = Crc32Table(lIndex)
'Then mix new Crc32 value with previous accumulated Crc32 value
Crc32 = lAccValue Xor lTableValue
End If
Next
'Return new Crc32 checksum
AdDDNc32 = Crc32
End Function
I ended up with this
public static long AdDDNc32(string item, long inCrc32)
{
long crc32 = inCrc32;
byte bCharValue;
int i;
long index;
long lAccValue;
long tableValue;
for (i = 0; i<item.Length;i++)
{
bCharValue = (byte) Microsoft.VisualBasic.Strings.Asc(item.Substring(i , 1));
if ((bCharValue > 0x1f) && (bCharValue < 0x7f))
{
lAccValue = crc32 & -256;
lAccValue = lAccValue / 0x100;
lAccValue = lAccValue & 0xFFFFFF;
index = crc32 & 0xFF;
index = index ^ bCharValue;
tableValue = ccitt_32[index];
crc32 = lAccValue ^ tableValue;
}
}
return crc32;
This is close and works on most strings passed in but fails at times, I
believe this is when characters like < > & ... get passed in.
Can anyone point out where I've gone wrong? I'd prefer to figure this out
to just turning the VB into a DLL