04-08-2015 09:04 PM
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!
Solved! Go to Solution.
04-08-2015 10:30 PM
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.
04-08-2015 10:50 PM
A very similar approach but using an array of clusters of 3 U8, from this thread a few years ago:
04-09-2015 08:35 AM
The code by Omar_II and nathand won't give correct results. Try the attached VI.
04-09-2015 09:04 AM
@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)
04-10-2015 12:58 PM
@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.
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?
04-10-2015 01:01 PM
Nevermind, had to change the output array of course! Thanks
04-10-2015 01:33 PM
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.
04-10-2015 01:54 PM
@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.