LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert HEX String into HEX Array

Hi!

Probably a silly question, but I am looking for a way to convert a ROM ID number such as "5E03C21000BA" into an short hexadecimal array.

The way to do this in C would probably be

char[17] str_romID = "5E03C21000BA";
uchar[8] romID;

sscanf(str_romID, "%02X%02X%02X%02X%02X%02X%02X%02X",
&romID[0], &romID[1], &romID[2], &romID[3],
&romID[4], &romID[5], &romID[6], &romID[7]);

but I can't seem to find a way to do this in LabVIEW. I would be very grateful if you could help me out with a sample program. Thanks ever so much!

Stefan
0 Kudos
Message 1 of 12
(6,863 Views)
Hi Stefan
Under Functions->strings->conversion functions, you will find a conversion function to do what you whant.
cheers
Pawel
0 Kudos
Message 2 of 12
(6,858 Views)
Hi Pawel!

Unfortunately, this only works for unsigned byte arrays.... I would like to convert this hex string to a byte hex array... i.e.: string "5E03C21000BA" to an 8-bit array "[5] [E0] [3C] [21] [0] [0] [0] [BA]". With the function you mention, I would get an 12-bit array "[5] [E] [0] [3] [C] [2] [1] [0] [0] [0] [B] [A]". Do you know a workaround for that?

Regards,
Stefan
0 Kudos
Message 3 of 12
(6,854 Views)
Hello Blondchen (hübscher Name, ohne jetzt allzuviel assoziieren zu wollen :-),

how do you convert your data? I have problems to understand your conversion...
You said: 5E03C21000BA to [5] [E0] [3C] [21] [0] [0] [0] [BA]
I would convert: 5E03C21000BA to [5e] [03] [c2] [10] [00] [ba]
So how should we know where to split your big number/string?

Anyhow, here's my advice: make a loop where you take two chars from the string (split string), convert those two chars with "hexadezimal string to number" to a number and output this number via an indexing enabled loop output to get your byte array. The "hex string to number" allows to connect a default values to set the desired datatype.

Hope this helps,
GerdW
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 12
(6,847 Views)
Hi GerdW!

Well, the name "Blondchen" probably really implies that I am quite a thickhead, as I still can't seem to find a solution.... All I know is, that I have to convert this string into a hex BYTE array. As I understand, this would mean an array containing 8 hex BITs... Your array in your example contains 6 bits only... that wouldn't be working then...

Many regards!
Stefan
0 Kudos
Message 5 of 12
(6,842 Views)
Hello Blondchen,

I have attached a picture to my last explanation. This example doesn't take care of strings with an uneven number of chars!

My array has only 6 numbers as you only gave a string with 12 chars... That's what I asked before: how should we know where to split your string to the single numbers? Normally I would take 2 chars to form a byte (as I did in the example). If you give me 16 chars I give you 8 bytes...
I also think your C byte array will be an U8 array in LabView. If you REALLY need I8 numbers you can change the default type of the "hex to string" or cast to I8 (advanced->data manipulation->type cast) after the conversion.

Best regards,
GerdW
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 12
(6,834 Views)
Hi,
Please find attached function useful

IFK_STring_NormStr2HexStr+Array.vi

Nevertheless, please do not use it without understanding how it works 😛
Or otherwise, you learn nothing!

Cheers!
ian.f koo
Ian F
Since LabVIEW 5.1... 7.1.1... 2009, 2010, 2014
依恩与LabVIEW
LVVILIB.blogspot.com
0 Kudos
Message 7 of 12
(6,833 Views)
Dear Ian and Gerd!

Thank you very much for your kind help. Unfortunately, your solutions don't quite work out, as the array which is to be produced, has to be exactly 8 bytes long. I need this array to address a Dallas-Semiconductor 1-wire sensor (better known as iButton). I have included a jpeg illustrating the way the function works.

The Program in C++ mentioned in my very first posting is creating the array correctly. I just don't know to translate it into "G"...

char[17] str_romID = "5E03C21000BA";
uchar[8] romID;

sscanf(str_romID, "%02X%02X%02X%02X%02X%02X%02X%02X",
&romID[0], &romID[1], &romID[2], &romID[3],
&romID[4], &romID[5], &romID[6], &romID[7]);

The correct array must therefore be: [5] [E0] [3C] [21] [0] [0] [0] [BA]

I know I am getting on your nerves... but please help me!
0 Kudos
Message 8 of 12
(6,813 Views)
Hello Blondchen,

you wrote: "sscanf(str_romID, "%02X%02X%02X%02X%02X%02X%02X%02X",..."
That's (in my opinion) the same in LabView when you use the "Scan From String" function. The format code says: take 2 chars (with leading zero) as a hex number. That's also what Ian and I did in our examples: take two chars and make a (hex) number of it. But your conversion is something different:
your first parameter is scanned (%02X) and you get [5]??? You should get [5E] with that scan code. Also the scanning of the zeros should give different results... Could it be your char[17] is wrong? You make an char array with 17 chars (in my C knowledge this are 16 chars and 1 NULL at the end). But you write char[17] = "5E03C21000BA"! That's only 12 chars as I mentioned before. To get the result you want to have your char[17] has to be "05E03C21000000BA"! Maybe you forgot to display leading zeros? In Labview you can have them by setting the format of a numeric display to hexadecimal with the desired number of digits AND the option "Display with leading zeros" (right-click a numeric and choose Format/Precision).

Hope this clarifies the difference in our coding examples.
GerdW
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 12
(6,808 Views)
At this point if I were you I would just write the C function to do it and compile it to a dll. Then just access the dll with a labview dll interface node. Sometimes you gotta do what you gotta do.
-Devin
I got 99 problems but 8.6 ain't one.
0 Kudos
Message 10 of 12
(6,794 Views)