473,385 Members | 1,942 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,385 software developers and data experts.

Converting VB to c# with bitwise operations

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


May 8 '06 #1
5 1710
"jamie" <st*********@nospam.nospam> wrote in message
news:e%****************@TK2MSFTNGP02.phx.gbl...
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
May 8 '06 #2
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" wrote:
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


May 8 '06 #3
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.

"Michael C" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"jamie" <st*********@nospam.nospam> wrote in message
news:e%****************@TK2MSFTNGP02.phx.gbl...
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

May 10 '06 #4
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" <Da********@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com... 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" wrote:
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


May 10 '06 #5
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.

"jamie" <st*********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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.

"Michael C" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"jamie" <st*********@nospam.nospam> wrote in message
news:e%****************@TK2MSFTNGP02.phx.gbl...
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


May 10 '06 #6

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

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
15
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
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: 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: 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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.