473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dealing with hex values in c# vs VB

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?


Jan 12 '06 #1
6 3236
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"jamie" wrote:
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?


Jan 12 '06 #2
"=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?="
<pb*******@yahoo.nospammin.com> wrote in
news:E5**********************************@microsof t.com:
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
Jan 12 '06 #3
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" <st*********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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?


Jan 12 '06 #4
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.

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?="
<pb*******@yahoo.nospammin.com> wrote in
news:E5**********************************@microsof t.com:
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

Jan 12 '06 #5
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.


"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?="
<pb*******@yahoo.nospammin.com> wrote in
news:E5**********************************@microsof t.com:
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

Jan 12 '06 #6
Correct. Thanks for the help
"Rocky" <no*****@nowhere.com> wrote in message
news:eO**************@TK2MSFTNGP15.phx.gbl...
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.

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?="
<pb*******@yahoo.nospammin.com> wrote in
news:E5**********************************@microsof t.com:
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


Jan 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Gerrans | last post by:
Is there an ideal approach to dealing with the FutureWarning about getting the hex value of a negative int? I'm using zlib.crc32() which can return a negative 32-bit int value, so for example...
5
by: Tom Willis | last post by:
How are the expert pythoneers dealing with config files? Is there anything similair to .net's config files or java's .properties? A quick search on google didn't return anything that looked...
20
by: Max Sandman | last post by:
I'm getting increasingly frustrated with C# and its exceptions on null values. Rather than try to deal with it on a hit-or-miss basis as exceptions pop up, I thought I should try to learn exactly...
3
by: Steve Weixel | last post by:
I'm having problems getting dates to format the way that I need them to. The problem is that I'm used to the VB6 way of doing things, with which the statement Format(37866, "dd MMM yyyy") would...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
6
by: Paul Rubin | last post by:
I'm thinking of proposing that a values method be added to set objects, analogously with dicts. If x is a set, x.values() would be the same as list(x). This feels logical, and it would allow...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
5
by: Paul Miller | last post by:
I'm looking at doing some currency calculations in some Python code integrated with a C++ application. I want to be able to come up with the same values I get in an Excel spreadsheet. I've been...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
10
by: ++imanshu | last post by:
Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. Thanks,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.