|
-
Jun 17th, 2008, 02:17 PM
#1
Thread Starter
New Member
Help with Conversion to Byte
I have a application I'm working on that requires the calculation of Checksum. The checksum can be calculated by adding up all the bytes of the data and subtraction that from 255(ie, 255 - sum of data). As one would except, the value of the sum of the data is, at times, higher than 255. This gives me a negative value to convert into a byte. It seems that VB.net won't do that.
I've previously done the calculation manually via Excel. Here's an example from excel:
The checksum ends up being: -1465
when converted to hex I get: FFFFFFFA47
The value I need for the checksum is the 47
Anybody have a suggestion?
-Regards
-
Jun 17th, 2008, 03:34 PM
#2
Re: Help with Conversion to Byte
Well, normally, I would say add everything up and mask off the high bytes by ANDing with &H000000FF, but that would be because you were only looking at the low byte. If you are content with the numbers going negative, that technique won't work because a negative number is represented by the twos complement.
Well, actually, it should still work, but it makes me queasy. If you perform that AND operation, the three high bytes will be cleared, and all you will have left is the contents of the low byte, which is the 47. Therefore, something like:
check = CByte((255 - sum) AND &H000000FF)
should do. The thing is that many people would put the parenthesis in a different place:
check = CByte(255-(sum AND & H000000FF))
which will give a much different answer. As long as you know which one you want, give it a try. The latter will keep the value from ever going negative, so you never see the twos complement issue, but you end up with a different checksum value.
My usual boring signature: Nothing
 
-
Jun 17th, 2008, 07:15 PM
#3
Re: Help with Conversion to Byte
Code:
Dim cksum As Byte
Dim bi As Integer = 0
Dim r As New Random
For x = 1 To 100
bi += r.Next(0, 255)
bi = bi And 255
Next
cksum = CByte(255 - bi)
Are the bytes added or XOR'ed?
Last edited by dbasnett; Jun 17th, 2008 at 07:20 PM.
-
Jun 17th, 2008, 11:18 PM
#4
Re: Help with Conversion to Byte
 Originally Posted by dbasnett
Code:
Dim cksum As Byte
Dim bi As Integer = 0
Dim r As New Random
For x = 1 To 100
bi += r.Next(0, 255)
bi = bi And 255
Next
cksum = CByte(255 - bi)
Are the bytes added or XOR'ed?
good point. If you are XORing them, they should never get above 255.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|