From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting hex string to hex array

Solved!
Go to solution

Hi Guys,

 

I needed a little help on one of my VIs. I am trying to receive data from my atmega micro controller through an FTDI chip on my PC. I am transmitting a 10 bit data frame from the micro controller with 1 start bit, 1 stop bit, no parity bit and 8 data bits with baud rate of 9600.  I want to convert the received hex string to hex array so that I can perform a few arithmetic operations on it. Here are the following problems:

1) I am transmitting 0x25 from the micro controller but I am unable to read the hex string as continuous 8 bit hex values as 0x25 but I get 2525 2525 2525...

2) Further to convert this string I am using "Spreadsheet String to Array", but that gives only 0 as the output. I tried connecting various data type constant to "format string" parameter but all of them output a zero. I also tried changing the data type of the indicator connected to it but nothing works.

Please refer to my VI and attached image.

 

Thanks a lot!

 

Download All
0 Kudos
Message 1 of 20
(6,240 Views)

Your "read buffer" variable is/* set to display hex. 2525 2525 is correct. When viewing a string control in Hex mode, it groups it into two byte values with a space between them. It doesn't format strings with the 0x prefix.

 

Your String to u8 array function is doing what you want here. It's showing 37, 37, 37,... Decimal 37 = hex 25. String to U8 array is the function you're looking for and you've already found it 🙂

0 Kudos
Message 2 of 20
(6,235 Views)

Thanks for your reply BertMcMahan,

 

The string to u8 array is the thing I am bit confused about. It always outputs a 0 for all the data representations I use. Please can you let me know if the default delimiter I am  is wrong or the "format string" parameter is incorrect.

Thanks!

0 Kudos
Message 3 of 20
(6,225 Views)
Solution
Accepted by topic author pratikpade

The picture you posted shows it outputting 37 over and over. Is that not the case?

 

Note: I'm talking about String to Byte array, which converts a string to a u8 byte array, NOT "Spreadsheet string to array".

 

The reason Spreadsheet string to array doesn't work is that you don't have a spreadsheet string. A spreadsheet string is one where you have an ASCII number, then a delimeter (the default is a tab character), then another number, then a delimeter, etc.

 

For example:

"1\t2\t3" is a spreadsheet string. The ASCII character 1, the tab character (represented by \t here, which is how it's shown if you right click a string control and click "'\' Codes Display")

 

Your actual data is 0x25 0x25 0x25. 0x25 is a % sign (see: http://www.asciitable.com/).

 

Spreadsheet String to Array looks for ASCII numbers, not binary ones. It will see 0x25... as "%%%%", which has no ASCII numbers nor does it have delimeters, so it will return an empty array.

 

If you need a STRING of 0x25 0x25 0x25 (where you have an ASCII 0, then ASCII x, then ASCII 2, then ASCII 5) you can do that- but I imagine you don't really need a string saying that, you need the hex value.

 

Short version: In the code you posted, the small function connected to "unsigned byte array" is the one you want to use. Right click on "unsigned byte array" and select Visible Items -> Radix. You'll see a small "d" show up, indicating the values are in decimal. Click the "d" and change it to "hex" and your array of 37 37 37 37 becomes 25 25 25 25.

Message 4 of 20
(6,215 Views)

Thanks for the detailed explanation. Yes I was looking for the last thing you mentioned.

Thanks a lot!

0 Kudos
Message 5 of 20
(6,206 Views)

Hi Guys,

 

I tried implementing the mentioned solution, but it is not working for me. I want to perform. Hex string to hex array conversion and later perform a bit of arithmetic and then plot the results.

Please refer to my attached VI. I am continuously sending 0x25 and and trying to convert it to array but I am receiving a blank array. Please let me know if you find the problem 
Thank You

0 Kudos
Message 6 of 20
(6,136 Views)

Hi, BertMcmahan,

 

To followup on this question. Can you suggest a method in which I can use the hex string and perform arithmetic operations on it? Then plot the results.

 

I am sending the data given by an adc, thus I want to convert it back to analog and then plot it. please can you let me know how can I do that?

Thank you!

0 Kudos
Message 7 of 20
(6,159 Views)

It's not a hex string- the data type is U32, which means it's a binary value. You can use all of LabVIEW's normal math functions on it directly, you don't have to do any conversions.

 

The reason behind this is that numbers are stored in the computer as binary. Your visualizations of the data in decimal or hex just affects how you're looking at it- not how it's actually represented in the computer.

 

As for plotting, there are some good examples in the LabVIEW Help and Example Finder that you can look through.

0 Kudos
Message 8 of 20
(6,157 Views)

What i want to do is implement a kind of DAC. When a value is read eg 0x25, the analog value is calculated as:

(Analog value)=(0x25/4095)x3.3

(Analog value)=0.020.

 

But for this operations I am unable to connect the string directly to a math function. I have to convert it to an array first. When I try to convert this input string to an array the 0x25 value is converted to 37 (which i know is the decimal equivalent). I can configure the indicator (connected to the string to byte array converter) to display hex value but I cannot use this hex value for arithmetic operations. I am stuck with 37 for further operations.

Another function I am using is hexadecimal string to number which is also unable to give me a hex value. 

 

In conclusion: I want the 252525 string to be a 2525 array for arithmetic operations.

0 Kudos
Message 9 of 20
(6,152 Views)

You do not want to convert anything. Your U32 values are fine- like I said, the values are not different in the computer.

 

Your equation is the value in hex divided by 4095- is that 4095 decimal or hex? I assume that's decimal, as 4096 is a power of 2 in decimal. Either way, use a Division function and drop a constant there. If you want to divide by (decimal) 4095, then type in 4095. If you want to divide by 0x4095, then right click the constant, show the Radix, change to hex, and type in 4095. Then you can multiply by 3.3- which, I am assuming, is a voltage value and is in decimal, so you should add a Multiply function and a constant value of 3.3 wired to it.

 

I think you're misunderstanding the difference between hex and decimal views of a value and the actual value on the wire. You already have what you want- 0x25 is exactly the same as 37 (in decimal). You cannot "convert" a wire between those data types- they are not strings, they are U32 values. "Hex" and "decimal" are only how the data is shown **to you**. The computer doesn't care about the differences between the two numbers, only the display. Hex and decimal are not units- they are representations. The radix display doesn't change how the computer actually handles the data, just how it's shown to you on the screen.

 

Your VISA Read function returns data as a string, which to the computer is basically an array of U8 values. Once you've converted the string to a U8 array, you're done. That's it. Just do your math on that array. No more conversions are needed.

 

I would recommend you create a VI that lets you type in a number into a numeric control, do some multiplication and division, and displays the answer. This should help you learn how to manipulate the data and what the difference is between "data types".

 

Last- you have no "hexadecimal strings" anywhere in your application. A hex string uses ASCII representations to write out a value. Your data is already numerical values and can be multiplied or divided at will.

0 Kudos
Message 10 of 20
(6,146 Views)