LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Hexadecimal String to Byte Array

Solved!
Go to solution

Hi,

 

I would like to convert an Hexadecimal String (e.g. 0x156FD18A4) to a Byte Array, the problem is that I do not have any delimiter (e.g 15 6F D1 8A 4) so I cannot use the Spreadsheet String To Array function, I tried to do something on my own, but everything seems to be too complicated is there any proper, clean and neat way to achieve what I want? (or maybe a smart trick to add delimiter every two characters in my original String)

 

Thanks!

0 Kudos
Message 1 of 29
(19,304 Views)

Use the LabVIEW inbuilt function "String To Byte Array"

0 Kudos
Message 2 of 29
(19,286 Views)

Please attach a VI containing a typical input string as a diagram constant.

 

In the input a hdexadecimally formatted string starting with "0x" and followed by a string if characters in the range A..F?

What is the max (or fixed) lenght of the string? Does it represent a numeric of known representation?

0 Kudos
Message 3 of 29
(19,283 Views)

@Vijay_J wrote:

Use the LabVIEW inbuilt function "String To Byte Array"


Ok seems I was not clear enough, I do not want the value of the ASCII characters so this "LabVIEW inbuilt function "String To Byte Array"" cannot work unless I do some tricks substracting a certain number but to me it seems a bit weirdo to do that (but true I could do that to some extent, 

A = 65 => 65 - 55 = 10, altough I don't really like the idea to rely on a blurry definition of what character set (i.e. encoding) is used through this conversion).

 

Here is a example of what I want to achieve:

Let's say my input is "0X465F66A54", ok I can trim the two first characters at beginnning of that Hexadecimal String and get something like that: "465F66A54".

 

Now I would like to convert it to the corresponding Byte Array matching the Hexadecimal Numbers represented by this Hexadecimal String aka:

46 5F 66 A5 (0 left padding)4

 

Here is the Bytes I want to get (0x is just here to indicate the radix)

0x46 [0]

0x5F [1]

0x66 [2]

0xA5 [3]

0x04 [4]

 

Is it clear enough now, or do I need to explain something else?

 

Oh last thing, about the string length, I don't really know but it can be pretty lon (e.g 42, 43 up to a few undreds of characters), and as it was introduced in the example above the string length is not necesserarly a multiple of two.

 

 

Thanks,

 

 

0 Kudos
Message 4 of 29
(19,264 Views)

 


@Nelval wrote:

@Vijay_J wrote:

Use the LabVIEW inbuilt function "String To Byte Array"


Ok seems I was not clear enough, I do not want the value of the ASCII characters so this "LabVIEW inbuilt function "String To Byte Array"" cannot work unless I do some tricks substracting a certain number but to me it seems a bit weirdo to do that (but true I could do that to some extent, 

A = 65 => 65 - 55 = 10, altough I don't really like the idea to rely on a blurry definition of what character set (i.e. encoding) is used through this conversion).

 

Here is a example of what I want to achieve:

Let's say my input is "0X465F66A54", ok I can trim the two first characters at beginnning of that Hexadecimal String and get something like that: "465F66A54".

 

Now I would like to convert it to the corresponding Byte Array matching the Hexadecimal Numbers represented by this Hexadecimal String aka:

46 5F 66 A5 (0 left padding)4

 

Here is the Bytes I want to get (0x is just here to indicate the radix)

0x46 [0]

0x5F [1]

0x66 [2]

0xA5 [3]

0x04 [4]

 

Is it clear enough now, or do I need to explain something else?

 

Oh last thing, about the string length, I don't really know but it can be pretty lon (e.g 42, 43 up to a few undreds of characters), and as it was introduced in the example above the string length is not necesserarly a multiple of two.

 

 

Thanks,

 

 


Please refer the attachment and VI snippet

 

Download All
0 Kudos
Message 5 of 29
(19,255 Views)

@Vijay_J wrote:

 


@Nelval wrote:

@Vijay_J wrote:

Use the LabVIEW inbuilt function "String To Byte Array"


Ok seems I was not clear enough, I do not want the value of the ASCII characters so this "LabVIEW inbuilt function "String To Byte Array"" cannot work unless I do some tricks substracting a certain number but to me it seems a bit weirdo to do that (but true I could do that to some extent, 

A = 65 => 65 - 55 = 10, altough I don't really like the idea to rely on a blurry definition of what character set (i.e. encoding) is used through this conversion).

 

Here is a example of what I want to achieve:

Let's say my input is "0X465F66A54", ok I can trim the two first characters at beginnning of that Hexadecimal String and get something like that: "465F66A54".

 

Now I would like to convert it to the corresponding Byte Array matching the Hexadecimal Numbers represented by this Hexadecimal String aka:

46 5F 66 A5 (0 left padding)4

 

Here is the Bytes I want to get (0x is just here to indicate the radix)

0x46 [0]

0x5F [1]

0x66 [2]

0xA5 [3]

0x04 [4]

 

Is it clear enough now, or do I need to explain something else?

 

Oh last thing, about the string length, I don't really know but it can be pretty lon (e.g 42, 43 up to a few undreds of characters), and as it was introduced in the example above the string length is not necesserarly a multiple of two.

 

 

Thanks,

 

 


Please refer the attachment and VI snippet

 


This does not work either: the output is a String Array (and not matter what value contained in are not really matching the representation of the bytes contained in the Hexadecimal String).

 

0 Kudos
Message 6 of 29
(19,245 Views)

@Vijay_J wrote:
Please refer the attachment and VI snippet 

So, why is the input string set to hex display?

If the lenght is odd, Is the right-most hex character always padded on the left with zero?

0 Kudos
Message 7 of 29
(19,229 Views)

Either entire string should be treated as Hex or Normal one all along

 

Output byte array for the input hex string "465F66A54” is as follows

 

0x46 [0] - > 70

0x5F [1] - > 95

0x66 [2] - > 102

0xA5 [3] - > 165

0x04 [4] - > 4

 

This is what exactly the "String To Byte Array" function does. Converting the Number/decimal/Unsigned integer to hex string is performed by "Number To Hexadecimal String".

 

Please refer the attachment. Hope I have conveyed, if not may be I did not understand your requirement correctly.

 

Thanks,

Vijay.

 

0 Kudos
Message 8 of 29
(19,230 Views)

@altenbach wrote:

@Vijay_J wrote:
Please refer the attachment and VI snippet 

So, why is the input string set to hex display?

If the lenght is odd, Is the right-most hex character always padded on the left with zero?


As a sample VI, control accepting hex string directly, otherwise LabVIEW will perform the ASCI conversion.

 

Second, I haven't considered the length as odd or even. It always segregates from least significant bytes.

0 Kudos
Message 9 of 29
(19,216 Views)

@Vijay_J wrote:

Either entire string should be treated as Hex or Normal one all along

 

Output byte array for the input hex string "465F66A54” is as follows

 

0x46 [0] - > 70

0x5F [1] - > 95

0x66 [2] - > 102

0xA5 [3] - > 165

0x04 [4] - > 4

 

This is what exactly the "String To Byte Array" function does. Converting the Number/decimal/Unsigned integer to hex string is performed by "Number To Hexadecimal String".

 

Please refer the attachment. Hope I have conveyed, if not may be I did not understand your requirement correctly.

 

Thanks,

Vijay.

 


I agree with comment made above about the hexadecimal display. When I receive the String, it is already a Hexadecimal Format why should I need to use the Hexadecimal of a String Control... that actually makes absolutely no sense at all! (especially when the value are converted to their ASCII equivalent), I though I made myself clear with my example.

 

But I'm struggling about how to make it even more clear now, it is quite crystal for me.

 

Here is attached an example but using a space delimiter as a separator in the Hexadecimal Input String, how to achieve the same with if I do not have the same separator between each Byte Representation in the orignal Hexadecimal String?

 

 

 

0 Kudos
Message 10 of 29
(19,219 Views)