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: 

Hex to decimal

Hello...Problems.....
 
I have been trying just about every conversion known to man in serial communicaiton. What am I missing.
 
I have a value 0007 0008 coming as a hex value. If you try converting it to a number, its value is 0 is labview.
I have even tried trimming it, but trim function treat two digits 00 07 00 08 as one, so it trims 00 instead of 0 and also tried trimming whitespace, but labview hex values do not contain whitespace. So 00 00 cannot be trimmed.ahhhhhhhh.
 
 I understand that the hex->number is reading the first value 00 and returning that as the number value. The only way the converter works is if the hex value was 0708 which is translates to the correct decimal value of 1800. What am I not doing? Or what do I need to do... Thanks.
 
0 Kudos
Message 1 of 16
(20,922 Views)


@j_es wrote:
What am I missing.

OK, first you need to tell us what kind of string you have. From you description it seems that you have a binary string and your text indicator is set to hex display. Here each raw character is displayed for your convenience and redability as two characters 0...F (00..FF for each byte) Each set of two bytes is split by a space, again for redability. The underlying raw string does NOT have any spaces.
 
Now we need to know what kind of data type you want. The hex data "0007 0008" could be read as four 8bit numbers (0, 7, 0, 😎 two 16 bit numbers (7, 😎 or one 32 bit number (458760).
 
It would have helped if you had attached your program attempt, because it is impossible to tell what you did to "convert".
 
I am guessing that typecast would be the correct function. Attached is a simple example. (LabVIEW 7.0)
 
(If, and only If, your string is a HEX formatted string that consists exclusively of the letters 0-F if the string is set to normal display you would use "hex string to number", for example.)
 
 
Message 2 of 16
(20,908 Views)
Hello Crosspost

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 3 of 16
(20,892 Views)
Im lost....I attached all my attemps. The best one so far is the code at the top.
 
About to do a case statement for each value in the hex string. Guess it would only be 15 cases.
 
Yes it is a hex text box of value 0007 0008...
 
The best thing i can do so far is use substring to get 00 07 00 08..
Then I used string to byte array to get 0 7 0 8. But there all seperate and each of the digits is in the first place in the array. It would be a lot easier to make the case statement if I continue down my path.
 
In the end I have to display 1800 for 0708 and then use it in equations.
 
 
Thanks for the response.

Message Edited by j_es on 08-14-2006 10:01 AM

0 Kudos
Message 4 of 16
(20,864 Views)
Nevermind. Couldnt do a case. Case statement doesnt read 00 hex.
0 Kudos
Message 5 of 16
(20,846 Views)

OK, let's back up for a second....

  • What's wrong with my typecast siuggestion?
  • Why do you need a case structure? (still, you can (almost) do a case that reads 00(hex). Just typecast your string to U8 before wiring to the case structure. Now right-click on the case structure and select "radix...hex".

99% of the solution is clearly defining the problem. Why don't you make a tiny example VI with your real input string as default value in a control. (Create a control containing your data, then right-click...data operations...make current value default", then save the VI).

Now include indicators that contain your desired output as defaults. ...and we go from there.

As I mentioned before, your use of "Hexadecimal string to number" is not appropriate here, because your string is not a hexadecimal formatted string, it e.g. contains the hex character x00. A hexadecimal formatted string has two characters for each byte and only the ASCII characters zero to F are allowed.

0 Kudos
Message 6 of 16
(20,835 Views)
Thanks altenbach for the help..
 
 
Finally got it to work. Im attaching the code just for future purposes.
 
 
Thanks again.

Message Edited by j_es on 08-14-2006 04:27 PM

Message Edited by j_es on 08-14-2006 04:28 PM

0 Kudos
Message 7 of 16
(20,807 Views)

Oops. That didntt work either. a-f didnt work.

Heres what works.

Just so I know the lingo.

What was coming in. To me it was just digits represented as hex in labview. Was this consider unicode or somehitng?

Message Edited by j_es on 08-14-2006 05:00 PM

0 Kudos
Message 8 of 16
(20,794 Views)
Granted, I don't know the details of your problem, but your solution seems a bit unusual.
 
It seems you are keeping only the low nibble of each byte, leaving you with 16 bits (x0708). Why do you turn it into an U32 then? U16 would be enough! Since you are throwing away the first four bits of each byte you will loose data for arbitrary inputs (e.g. x1F2FAFBF would turn into xFFFF). Are you sure that's right?
Anyway, you jump through too many flaming hoops, the same could be done much simpler. (Method 1).
 
You can also do the same using logic and shifts (method 2)
 
(You would also get the same result for your input example of you simply combine bytes 2 and 4. (Method 3) Of course for arbitrary inputs the result might be different.)
 
 
There are many more ways to do this, and all are simpler than your approach. 😉

Message Edited by altenbach on 08-14-2006 03:24 PM

Download All
Message 9 of 16
(20,789 Views)
Process 2 doesnt work for all hex values a-f and 4 doesnt do it. Didnt check 3.
 
Thanks for try though.
0 Kudos
Message 10 of 16
(20,758 Views)