LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how do i add up hex bytes in labview 7?

hello - i am receiving around 11 bytes of data from a microcontroller and at the end need to check the checksum value. how do i add these bytes (in hex i guess) to equal FF in hex: doing this in labview 7.0.

thanks!
0 Kudos
Message 1 of 18
(4,625 Views)
Are you receiving hex bytes as characters? ASCII characters representing the hex digits like "FF"? Or a character whose ASCII code is the byte (like a weird character or unprintable character)? For the first case, you should convert each 2 digit hex string to a number using the Hexidecimal String to Number function found in the Functions palette - String - String/Number Conversion. If the second case, use a typecast function to convert each byte into a number. After you convert each hex byte into a number, just add the numbers. Usually for checksum, you add the numbers and then perform an AND with FF. You can wire the sum into one input of the AND function (found in Boolean palette) and wire 255 into the other input. The output will be the checksum.
- tbob

Inventor of the WORM Global
0 Kudos
Message 2 of 18
(4,621 Views)
it is the second case - so after i convert the ascii to regular numbers by using the typecast and adding them - do i convert the sum back into ascii to check to see if the result is "FF" ? or...should i just try to use the AND function you mentioned (haven't used it yet)

thanks....
0 Kudos
Message 3 of 18
(4,617 Views)
is 255 in decimal format or hex? what is 11111111 ? how do i know if it equals FF ? thanks 🙂
0 Kudos
Message 4 of 18
(4,613 Views)


@NewMachine wrote:
is 255 in decimal format or hex? what is 11111111 ? how do i know if it equals FF ? thanks 🙂


255 is in decimal and it is equal to FF and to 11111111. To modify the view on your numeric control, right click it and select Visible items>>Radix. Then, you can use the radix to select the format.
A quick explanation:
All these systems, including decimal, work in the same way:
each digit is worth base to the power of its place and then you add them.
In base 10, 536 is 5x10^2+3x10^1+6x10^0.
In base 2 (binary), you only have 1 and 0, which you multiply by powers of 2, so 8 times 1 equals 1x2^7+1x2^6... which is 255.
In base 16 (hex), you have 16 digits (0-9,A-F), and the same principle applies.
So, FF is Fx16^1+Fx16^0. Since F is 15, that gives you 255.
I hope this was clear enough.

___________________
Try to take over the world!
0 Kudos
Message 5 of 18
(4,607 Views)
Once you have the sum of the bytes (a number), AND it with decimal 255, which is the same as FF. These are numbers, not strings. The result is the calculated checksum number. You can use the equals function to compare it to an expected checksum. Wire the calculated checksum in one terminal and the expected checksum in the other (255 in your case) and the output will be true if the two numbers are equal, false if they are not. After converting strings or hex bytes into numbers, you can work with numbers. No need for strings anymore. Whether you want to display the numbers in decimal (255) or hex (FF) makes no difference, the numbers are the same.
- tbob

Inventor of the WORM Global
0 Kudos
Message 6 of 18
(4,601 Views)
In base 2 (binary), you only have 1 and 0, which you multiply by powers of 2, so 8 times 1 equals 1x2^7+1x2^6... which is 255.

where does the 7 and 6 come from in the exponent of base 2? how does that represent 8? 🙂
0 Kudos
Message 7 of 18
(4,406 Views)
I am not understanding how to use AND.....it seems that I only need to use the equal comparison (to 255). I would like to use the AND - but it seems to return a number, not a boolean value. How do I use the output of the AND and how would it help? Thanks!
0 Kudos
Message 8 of 18
(4,582 Views)

NewMachine wrote: where does the 7 and 6 come from in the exponent of base 2? how does that represent 8? 🙂

All bases start with a 0 and have X digits. Base 2 has 0 and 1 and base 10 has 0..9. The 1x2^7 represents the 8th (or first, depends which way you look at it) digit, because the least significant place always has NxB^0, where B^0 is always equal 1. It's the same in base 10. A 3 digit number starts with Nx10^2, not 10^3.
Here are a couple of links I quickly googled if you want more: 1, 2 and 3.

Message Edited by tst on 03-18-2005 10:03 AM


___________________
Try to take over the world!
0 Kudos
Message 9 of 18
(4,383 Views)
I don't think New Machine is familiar with binary or hex numbering systems. 1x2^7 means 1 times 2 raised to the 7th power, which is 2x2x2x2x2x2x2 which is 128 in decimal, which is the same 80 in hex (8x16+0=128).

New Machine:
Please learn the numbering systems first of all. Also, with Labview, the AND funtion is not just for booleans. You can AND two numbers. It will do a binary AND of each bit. For example 255 decimal (FF in hex) in binary is 11111111. When computing checksums, only the last eight bits are used. That is why you AND your byte sum with FF. For example, lets say your bytes add up to 257. In binary this is 100000001. Notice that there are 9 bits. If you use the AND function with FF, it is like 100000001 AND 011111111 (pad extra zero at the left to make 9 bits). The result of ANDing all the bits is 000000001 which is simply 1, your checksum is 1. To get a checksum of FF, your byte sum would have to have a binary representation of anything for bits 8 and above, and all ones for bits 0 through 7, like 1010101011111111 AND 0000000011111111. ANDing these two will produce 11111111 which is FF in hex (255 in decimal). Learn these numbering systems should be your first priority.
- tbob

Inventor of the WORM Global
0 Kudos
Message 10 of 18
(4,359 Views)