C to VB.NET

emresamisuzer
emresamisuzer used Ask the Experts™
on
I need the following code to be converted to vb.net code.

unsigned short compute_crc(unsigned char* bufptr, int len)//where len is the length of the packet excluding the crc bytes.
{
	
	unsigned short chr_bit=0;
	unsigned short crc_bit=0;
	unsigned short checkSumResult=0;

	char chr='0';
	unsigned short bit_cnt=0;
	if (len>0)
		{
		do
		{
			chr = *bufptr++;

			for(bit_cnt=0;bit_cnt<8;bit_cnt++)
			{
				chr_bit = (unsigned short) (chr & 0x01);
				crc_bit = (unsigned short) (checkSumResult & 0x01);
				checkSumResult >>= 1;
				chr >>= 1;
				if (0 != (chr_bit ^ crc_bit))
				{
					checkSumResult ^= 0x8408;
				}
			}
		}while(--len);		// repeat until end of string
		}

	return(checkSumResult);	// return resultant crc
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
   Private chr_bit As Short = 0
   
    Private crc_bit As Short = 0
   
    Private checkSumResult As Short = 0
   
    Private chr As Char = Microsoft.VisualBasic.ChrW(48)
   
    Private bit_cnt As Short = 0
   
    Private Function compute_crc(ByVal char As unsigned) As Short
Dim len As Integer
If (len > 0) Then
    bufptr = (bufptr + 1)
    chr = bufptr
    bit_cnt = 0
    Do While (bit_cnt < 8)
        chr_bit = unsigned
        Dim Unknown As Short
        (chr And 1)
        crc_bit = unsigned
        Dim Unknown As Short
        (checkSumResult And 1)
        1
        1
        If (0  _
                    <> (chr_bit Or crc_bit)) Then
            Dim = As checkSumResult
            'The operator should be an XOR ^ instead of an OR, but not available in CodeDOM
            33800
        End If
        bit_cnt = (bit_cnt + 1)
    Loop
   
    While
       
    End While
    len
    ' repeat until end of string
End If
Return


    End Function
here is a link to a free website that will convert  between c# and vb in both directions.

http://www.developerfusion.com/tools/convert/csharp-to-vb/

Author

Commented:
scottlafoy, I need a conversion from c to vb.net not from c#.

rkworlds, there are some lines that I could understandt like Dim = As checkSumResult.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Commented:
Can I ask a silly question or three?

Is the input you want CRCing a file, or,

if not, do you have any objections to turning it into a file for computing the checksum?

Are you particularly tied to your checksumming algorithm?

I ask all of those because Windows has checksumming functionality built-in to it, and it therefore might save you time/errors to use this instead of rolling your own.

Commented:
Well, this seems to work - although it would be interesting to know the answers to my other question.

The snippet shows a button_click handler, which calls the VB function in chksum.vb.txt - so some simple cut 'n paste to test it is all that's required.

The other attachment is the C version with a little test 'main()'

You'll see that I've used the same input string in each case - plus I've tested it with a few others.


Private Sub Button1_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles Button1.Click

    Dim s As String = "Now is the time for all good men to come to the aid of the party"

    MsgBox(Hex(compute_crc((New System.Text.ASCIIEncoding()).GetBytes(s))))

End Sub

Open in new window

chksum.c
chksum.vb.txt

Author

Commented:
peetm, thanks for your response but have a problem.

I am using to create a CRC for serial communication. The data that is passed is "41 7E 8C 97 4C 00 00 01 00". This data has a CRC of "64 D0" but I couldn't have it is generated.

I have found that c code on the protocol documentation but I couldn't get this CRC value with this code. Also your code has returned another CRC 25F2

Commented:
Please provide the links.

Did you try the C code on the data you give here - I'm unclear as to whether you've tried it or not?
Commented:
Well, I just tried the C code on "41 7E 8C 97 4C 00 00 01 00" and it returns the correct result, i.e., D064, i.e., the same but with byte ordering the other way around.

C running checksum output:
538D
C547
781A
5F95
4B13
2251
432E
D9B6
D064


My VB translation will also produce the same result if you change

Loop While pos <= len

To

Loop While pos < len

VB running checksum:
538D
C547
781A
5F95
4B13
2251
432E
D9B6
D064

It was like that as I was previously using a C null terminated string as input.


Commented:
The C input

    unsigned char p[] = {0x41, 0x7E, 0x8C, 0x97, 0x4C, 0x00, 0x00, 0x01, 0x00, };

    printf("%X\n", compute_crc(p, sizeof(p)));

The VB input

    Dim ba() As Byte = {&H41, &H7E, &H8C, &H97, &H4C, &H00, &H00, &H01, &H00}

    MsgBox(Hex(compute_crc(ba)))


Author

Commented:
Thank you very much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial