LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

changing HEX into a number



NewMachine a écrit: ...but i was told when i use a string subset function and the input is hex code it reads two characters at a time, for one byte. i know it is coming back as hex from the board because when i put an indicator down....it is hex that is displayed.
I made both of the changes as suggested by you below and then type in F9FFFF and am returned 0...




You are still confusing a single byte with its displayed value which uses two hex chars between 00 and FF in hexadecimal representation (and would use 3 chars between 000 and 255 in decimal representation and 8 from 0000 0000 to 1111 1111 in binary).

For instance if you receive a single byte of decimal value say 65 (uses 2 decimal digits) it will be displayed as normal string as "A" (one alphanumeric digit) or "41" as hexadecimal representation (two hex digits).

The conversion of a n bytes number to decimal involves the following operations : multiply the left byte by 255*255, the medium byte by 255 and add both to the right byte. Fortunately, using the type cast operator, as indicated by Darren and used also by Ian Fung, the conversion is done in a more straightforward manner !

May be the attached example (LV 7.0) will help you to get a better understanding of your problem : it convert a string (displayed as hex) into an array of U32, displayed as hex and as decimal values.
Chilly Charly    (aka CC)
0 Kudos
Message 11 of 17
(1,350 Views)


NewMachine a écrit:
and what if i want to convert more or less than 3 bytes? i have one stupid byte that i try to convert using something like the attached vi...and it it doesn't return anything correct


Here you didn't confuse string hex values and hex representation, but you repeated your second mistake : you asked labVIEW to convert a single byte string into a two bytes numeric. Of course, LV must add an empty byte. No luck : the byte is added on the wrong side ! ;):D

I have modified your vi to enlight the effect of a too short or too long string on the conversion.

So, remember to either padd the string with the appropriate number of zeroes or slice it at the right length before converting to a numeric which uses a different byte width.
Chilly Charly    (aka CC)
Message 12 of 17
(1,346 Views)

hello - I don't understand becuase when I enter 0x01FF into the program you attached, it doesn't seem to return anything. I was expecting a numerical value of 511 in that case...thanks.....

 


@chilly charly wrote:


NewMachine a écrit: ...but i was told when i use a string subset function and the input is hex code it reads two characters at a time, for one byte. i know it is coming back as hex from the board because when i put an indicator down....it is hex that is displayed.
I made both of the changes as suggested by you below and then type in F9FFFF and am returned 0...




You are still confusing a single byte with its displayed value which uses two hex chars between 00 and FF in hexadecimal representation (and would use 3 chars between 000 and 255 in decimal representation and 8 from 0000 0000 to 1111 1111 in binary).

For instance if you receive a single byte of decimal value say 65 (uses 2 decimal digits) it will be displayed as normal string as "A" (one alphanumeric digit) or "41" as hexadecimal representation (two hex digits).

The conversion of a n bytes number to decimal involves the following operations : multiply the left byte by 255*255, the medium byte by 255 and add both to the right byte. Fortunately, using the type cast operator, as indicated by Darren and used also by Ian Fung, the conversion is done in a more straightforward manner !

May be the attached example (LV 7.0) will help you to get a better understanding of your problem : it convert a string (displayed as hex) into an array of U32, displayed as hex and as decimal values.


0 Kudos
Message 13 of 17
(1,319 Views)
NewMachine:  CC's vi type casts the input hex string into a U32.  In order to have the correct output, you must supply a 32 bit number as the input.  A 32 bit number uses 8 hex digits, so you must type in 8 hex digits in the string control.  01FF is only four hex digits (16 bit integer).  If you type in 000001FF, you will get the correct response.  You need to recognize that U8 is 2 hex digits, and U16 is 4 hex digits, and U32 is 8 hex digits.  You must use the correct number of hex digits when converting to a number.
- tbob

Inventor of the WORM Global
Message 14 of 17
(1,305 Views)
okay...it makes sense, but when i made the changes in the example program and type 01FF nothing is converted. ill attach the vi i used...thanks.....samantha
0 Kudos
Message 15 of 17
(1,297 Views)
But now you are trying to convert 4 hex digits to a U8. Make the representaion U16 like tbob said and it will work.
0 Kudos
Message 16 of 17
(1,290 Views)
I have modified your vi to do 3 different conversions (U8 U16 U32) and show you the number and the number in hex. Enter different Hex strings of different lengths and you will see the effect on the different conversions.
Andrew Alford
Production Test Engineering Technologist
Sustainable Energy Technologies
www.sustainableenergy.com
0 Kudos
Message 17 of 17
(1,282 Views)