LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Hex string to binary

Solved!
Go to solution

I have 33 bytes of data in hex and am not sure how to parse it into eleven 24-bit pieces of binary data. I have tried various configurations of Hexadecimal string to number and Type Casting but have not yet been successful. I'd like an array with 11 elements, each consisting of the 24-bit binary. 

 

The input is formatted as such: 

(Hex Display)
4445 4647 4849 4A4B 4C4D 4E4F 5051 5253 5455 5657 5859 5A5B 5C5D 5E5F 6061 AAAB AC

 

Please let me know if any more info is needed, thank you for the help!

0 Kudos
Message 1 of 9
(5,013 Views)
Solution
Accepted by topic author bpt2016

There is not a 24 bit data type so you will need to store it in U32. You need to "pad" your data to get it to fit right.

Hex string to binary.png

 

 

Omar
Message 2 of 9
(4,987 Views)

A very similar approach but using an array of clusters of 3 U8, from this thread a few years ago:

24 bit values in string to array of I32.png

Message 3 of 9
(4,973 Views)

The code by Omar_II and nathand won't give correct results.  Try the attached VI.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 4 of 9
(4,904 Views)

@hp43 wrote:

(Hex Display)

4445 4647 4849 4A4B 4C4D 4E4F 5051 5253 5455 5657 5859 5A5B 5C5D 5E5F 6061 AAAB AC


You said the string was in HEX Display.  Nathand and I assumed the string you posted was in HEX display and NOT NORMAL display. Which means the actural data is 

 

DEFGHIJKLMNOPQRSTUVWXYZ[\]^_`aª«¬  (viewed in normal) 33 bytes with NO SPACES

 

I think you copied your string from a HEX display and pasted it in your VI in a NORMAL display string control.

Which I do not think is how it is going to be in your final program.

 

Where does your data come from? Do you get the string with spaces added between each word (2 bytes)

Or as I supect you are just getting 33 raw bytes (without any spaces added)

Omar
0 Kudos
Message 5 of 9
(4,888 Views)

@Omar_II wrote:

There is not a 24 bit data type so you will need to store it in U32. You need to "pad" your data to get it to fit right.

Hex string to binary.png

 

 



Thanks, Omar. You are correct that I was simply showing hex display. This works well except I cannot figure out how to make the output allow signed number representation to allow for negative numbers. I have tried changing the type into typecast to I32 but no luck. Any ideas? 

0 Kudos
Message 6 of 9
(4,836 Views)

Nevermind, had to change the output array of course! Thanks

0 Kudos
Message 7 of 9
(4,835 Views)

Are you sure that's working for negative numbers? You should need to sign-extend the 24-bit value to 32-bits, which won't happen with Omar_II's code. That is, to get a negative number, the first 9 bits will need to be 1. In Omar_II's code, that will never happen. The first 8 bits will always be 0. That's the reason for the slightly more complicated logic in my code, with the x2^n function - it will preserve the sign properly.

0 Kudos
Message 8 of 9
(4,816 Views)

@nathand wrote:

Are you sure that's working for negative numbers? You should need to sign-extend the 24-bit value to 32-bits, which won't happen with Omar_II's code. That is, to get a negative number, the first 9 bits will need to be 1. In Omar_II's code, that will never happen. The first 8 bits will always be 0. That's the reason for the slightly more complicated logic in my code, with the x2^n function - it will preserve the sign properly.


Ah right, I did have to add the right shift after the type cast as well. Forgot to mention that in my last post. 

Capture.PNG

0 Kudos
Message 9 of 9
(4,799 Views)