Converting VB to c# with bitwise operations

J

jamie

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
 
M

Michael C

jamie said:
I'm having a hell of a time figure out how to translate this piece of
code.

That's quite a bit of code. Do you want us to do your job for you? :)

Michael
 
G

Guest

The main problem you have is that the C# bitwise operators are not && or ||.
Our Instant C# VB to C# converter produces the following (note that
conversion of 'On Error Resume Next' is not supported):

public long AdDDNc32(string Item, long Crc32)
{

//Declare following variables

byte bCharValue = 0;
int iCounter = 0;
long lIndex = 0;

long lAccValue = 0;
long lTableValue = 0;

//TODO: INSTANT C# TODO TASK: The 'On Error Resume Next' statement is not
converted by Instant C#:
On Error Resume Next

//Iterate through the string that is to be checksum-computed

int tempFor1 = Item.Length;
for (iCounter = 1; iCounter <= tempFor1; iCounter++)
{

//Selects the current character and converts it to an ASCII value

bCharValue = System.Convert.ToInt32(Item[iCounter - 1]);

//Only update CRC for valid ASCII characters, 32 to 126

if ((bCharValue > 31) & (bCharValue < 127))
{

//Right shift an Unsigned Long 8 bits

lAccValue = Crc32 & 0XFFFFFF00;

lAccValue = (int)System.Math.Floor(lAccValue / 0X100);

lAccValue = lAccValue & 0XFFFFFF;

//Now select the right adding value from the holding table

lIndex = Crc32 & 0XFF;

lIndex = lIndex ^ bCharValue; //inverts the byte

lTableValue = Crc32Table(lIndex);

//Then mix new Crc32 value with previous accumulated Crc32 value

Crc32 = lAccValue ^ lTableValue;

}

}

//Return new Crc32 checksum

return Crc32;

}

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



jamie said:
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
 
J

jamie

Actually this is an attempt for me to figure out how bitwise operations
work. I turned the VB into a DLL and used it that way awhile ago to keep on
schedule for work. Now that I've got some free time I'm trying to actually
understand it.
 
J

jamie

That doesn't make sense.
if ((bCharValue > 31) & (bCharValue < 127)) This should be a logical
compare. I want to know that the asci character code is between 31 and
127. Thanks for the help though.
David Anton said:
The main problem you have is that the C# bitwise operators are not && or
||.
Our Instant C# VB to C# converter produces the following (note that
conversion of 'On Error Resume Next' is not supported):

public long AdDDNc32(string Item, long Crc32)
{

//Declare following variables

byte bCharValue = 0;
int iCounter = 0;
long lIndex = 0;

long lAccValue = 0;
long lTableValue = 0;

//TODO: INSTANT C# TODO TASK: The 'On Error Resume Next' statement is not
converted by Instant C#:
On Error Resume Next

//Iterate through the string that is to be checksum-computed

int tempFor1 = Item.Length;
for (iCounter = 1; iCounter <= tempFor1; iCounter++)
{

//Selects the current character and converts it to an ASCII value

bCharValue = System.Convert.ToInt32(Item[iCounter - 1]);

//Only update CRC for valid ASCII characters, 32 to 126

if ((bCharValue > 31) & (bCharValue < 127))
{

//Right shift an Unsigned Long 8 bits

lAccValue = Crc32 & 0XFFFFFF00;

lAccValue = (int)System.Math.Floor(lAccValue / 0X100);

lAccValue = lAccValue & 0XFFFFFF;

//Now select the right adding value from the holding table

lIndex = Crc32 & 0XFF;

lIndex = lIndex ^ bCharValue; //inverts the byte

lTableValue = Crc32Table(lIndex);

//Then mix new Crc32 value with previous accumulated Crc32 value

Crc32 = lAccValue ^ lTableValue;

}

}

//Return new Crc32 checksum

return Crc32;

}

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



jamie said:
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
 
J

jamie

OK I've figured it out. Only took most of the morning. It turns out that
my code is correct, Earlier in the program the list of values that go into
ccitt_32 get calculated. That had an off by one error in the loop and
ccitt_32[255] never got set so it was a 0. This would fail consistantly on
some data but passed the vast majority of the time.

I still really don't understand whats going on in the code though.
 

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