dealing with hex values in c# vs VB

J

jamie

ok I'm attempting to implement a basic crc32 algorithm that I have the
original in VB and need to implement it in c#.

I appear to be running into an issue with the hex values when doing the bit
shifting. Anyways here is one of the functions in VB. This one builds a
polynomial table incase anyone cares.

'Public function to build polynomial table (don't touch!!)

Public Function InitCrc32(Optional ByVal Seed As Long = &HEDB88320, Optional
ByVal Precondition As Long = &HFFFFFFFF) As Long

'Declare counters

Dim iBytes As Integer, iBits As Integer, lCrc32 As Long, lTempCrc32 As Long

On Error Resume Next

For iBytes = 0 To 255

lCrc32 = iBytes

For iBits = 0 To 7

'Right shift unsigned long 1 bit

lTempCrc32 = lCrc32 And &HFFFFFFFE

lTempCrc32 = lTempCrc32 \ &H2

lTempCrc32 = lTempCrc32 And &H7FFFFFFF

'Now check if temporary is less than zero and then mix Crc32 checksum with
Seed value

If (lCrc32 And &H1) <> 0 Then

lCrc32 = lTempCrc32 Xor Seed

Else

lCrc32 = lTempCrc32

End If

Next

'Put Crc32 checksum value in the holding array

Crc32Table(iBytes) = lCrc32

Next

'After this is done, set function value to the precondition value

InitCrc32 = Precondition

End Function

And here is what I translated it into. Some of the variable names have
been changed.

public long initCRC32()

{

long seed = 0xEDB88320;

long precondition = 0xFFFFFFFF;

int bytes;

int bits;

long crc32;

long tempCrc32;

for (bytes = 0; bytes<255;bytes++)

{

crc32 = bytes;

for (bits = 0; bits < 8; bits++)

{

tempCrc32 = crc32 & 0xFFFFFFFE;

tempCrc32 = tempCrc32 / 0x2;

tempCrc32 = tempCrc32 & 0x7FFFFFFF;

if((crc32 & 0x1) != 0)

{

crc32 = tempCrc32 ^ seed;

}

else

{

crc32 = tempCrc32;

}

}

ccitt_32[bytes] = crc32;

}

return (precondition);

}

It runs but the values going into the table don't match. Walking through
the debugger with a watch on the seed variable right away it is different so
it's not a logic issue that I can see it's gotta be syntax .Anyone have any
idea on what I'm doing wrong?
 
G

Guest

Jamie,
There are a couple of (I think) better options:

1) Compile the VB.NET and use Lutz Roeder's Reflector freeware product to
decompile it to C#. Its' not perfect, but it works for most classes.
2) Don't "reinvent the wheel". Search google or MSN and find one written in
C# (There are plenty).

Peter
 
M

Michael Bray

1) Compile the VB.NET and use Lutz Roeder's Reflector freeware product
to decompile it to C#. Its' not perfect, but it works for most
classes.

Or for that matter, compile the VB.NET in a DLL and reference it. :)

As an aside, if the OP is using CRC only within his own applications (ie
not communicating with other applications that he didn't write) he could
also use MD5.ComputeHash(...). It works nicely because you can put the
result into a Guid.

-mdb
 
S

Stoitcho Goutsev \(100\)

jamie,

I can't see the actuall problem beside that I don't understand the choice of
variable types. All your numbers are 32 bits positive numbers. I don't see
why you use longs. Ofcourse I don't see the rest of the code, so I take it
there is a reason behind this.

Idealy I'd use unsigned types, but thery are not CLI compliant.
As I can see your numbers are positive, thus you don't need to zero that
last bit bit as well it is unnecessary to zero the first bit before shift;
you loose it anyways. I'd also use the right shift operator >>. This will
work because the numbers are positive.

These three lines of code:
lTempCrc32 = lCrc32 And &HFFFFFFFE

lTempCrc32 = lTempCrc32 \ &H2

lTempCrc32 = lTempCrc32 And &H7FFFFFFF

could be replaced with

lTempCrc32 = lCrc32 >> 1;

Ofcourse I might be missing something.

It would be great if you can poste working VB and C# samples that we can try
and compare the results.



--

Stoitcho Goutsev (100)



jamie said:
ok I'm attempting to implement a basic crc32 algorithm that I have the
original in VB and need to implement it in c#.

I appear to be running into an issue with the hex values when doing the
bit shifting. Anyways here is one of the functions in VB. This one builds
a polynomial table incase anyone cares.

'Public function to build polynomial table (don't touch!!)

Public Function InitCrc32(Optional ByVal Seed As Long = &HEDB88320,
Optional ByVal Precondition As Long = &HFFFFFFFF) As Long

'Declare counters

Dim iBytes As Integer, iBits As Integer, lCrc32 As Long, lTempCrc32 As
Long

On Error Resume Next

For iBytes = 0 To 255

lCrc32 = iBytes

For iBits = 0 To 7

'Right shift unsigned long 1 bit

lTempCrc32 = lCrc32 And &HFFFFFFFE

lTempCrc32 = lTempCrc32 \ &H2

lTempCrc32 = lTempCrc32 And &H7FFFFFFF

'Now check if temporary is less than zero and then mix Crc32 checksum with
Seed value

If (lCrc32 And &H1) <> 0 Then

lCrc32 = lTempCrc32 Xor Seed

Else

lCrc32 = lTempCrc32

End If

Next

'Put Crc32 checksum value in the holding array

Crc32Table(iBytes) = lCrc32

Next

'After this is done, set function value to the precondition value

InitCrc32 = Precondition

End Function

And here is what I translated it into. Some of the variable names have
been changed.

public long initCRC32()

{

long seed = 0xEDB88320;

long precondition = 0xFFFFFFFF;

int bytes;

int bits;

long crc32;

long tempCrc32;

for (bytes = 0; bytes<255;bytes++)

{

crc32 = bytes;

for (bits = 0; bits < 8; bits++)

{

tempCrc32 = crc32 & 0xFFFFFFFE;

tempCrc32 = tempCrc32 / 0x2;

tempCrc32 = tempCrc32 & 0x7FFFFFFF;

if((crc32 & 0x1) != 0)

{

crc32 = tempCrc32 ^ seed;

}

else

{

crc32 = tempCrc32;

}

}

ccitt_32[bytes] = crc32;

}

return (precondition);

}

It runs but the values going into the table don't match. Walking through
the debugger with a watch on the seed variable right away it is different
so it's not a logic issue that I can see it's gotta be syntax .Anyone
have any idea on what I'm doing wrong?
 
R

Rocky

Its because &HEDB88320 in VB is an Integer and not a Long. If you replace it
with 3988292384 it will produce the same numbers as the C# program.
 
J

jamie

The resulting CRC must match up against a CRC generated by someone elses
program so I'm stuck with this seed and algorithm. I personally would have
gone directly with the MD5 function but the choice was made long before I
started writing.

I may just DLL it but I'm curious as to where the differences in the
language are. Using the reflector program I managed to get the table
built correctly. It was a case of the hex values not directly transfering
between the languages. I'm now moving onto the second function that
actually uses the table.

the original vb

'CRC computation function

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


The line that is causing me issues is

bCharValue = Asc(Mid$(Item, iCounter, 1))

Mid$(Item,iCounter,1) should I think turn into item.substring(icounter,1)
but I'm not sure how to recreate the Asc function or exactly what it does.
 
J

jamie

Correct. Thanks for the help
Rocky said:
Its because &HEDB88320 in VB is an Integer and not a Long. If you replace
it with 3988292384 it will produce the same numbers as the C# program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top