LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

convert hexadecimal output to decimal (number)

I am reading hexadecimal format from the output of the VISA read block and I want to convert it to the decimal preferably exponential format.
Example of what I read is 76 00 02 (in dec: 7.600 E+2).
I know there is "type cast" block that converts nicelly but I am not sure what to specify for a type input.
Can you show me how to apply this type cast block for in my VI so that it converts hex string (76 00 02) into exponential string/number (7.600 E+2)?

I attached my VI so it is more clear.

Thanks everybody,
Pedja
0 Kudos
Message 1 of 29
(4,450 Views)
Coop wrote:

> I am reading hexadecimal format from the output of the VISA read block
> and I want to convert it to the decimal preferably exponential format.
> Example of what I read is 76 00 02 (in dec: 7.600 E+2).
> I know there is "type cast" block that converts nicelly but I am not
> sure what to specify for a type input.
> Can you show me how to apply this type cast block for in my VI so that
> it converts hex string (76 00 02) into exponential string/number
> (7.600 E+2)?

Looking at your code I see that you have not a hexadecimal string coming
back but instead a binary string. The fact that you display the string
as hexadecimal in the string indicator is just showing you the ASCII
codes of the string characters as hexadecimal numbers.

The funct
ion From Hexadecimal String expects a clear text string with
hexadecimal characters but that is something entirely different from a
binary string you choose to display as hexadecimal numbers.

An example: the binary string 7600 02 you receive from the device is in
fact the character 'v', a null character and a 02 character (or in ASCII
terms a start of text character).

So assuming your example is right (which I have to admit seems very
strange and therefore suspicious to me) you would have to do something
more like included example. But as I said I have little understanding
why the hexadecimal interpretation of the binary values should
correspond to the decimal value you want to have!

Also as you can see there is not really clear where the 00 character
belongs to. The two methods where it is either part of the multiplier or
the exponent both produce the same result for this particular example.
Which one is right if any, should be tested.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
Message 2 of 29
(4,444 Views)
Hi Rolf,

I agree now that I am receiving the binary string at the output of the VISA Read.

The string that I receive is in that format because the instrument sends it in the binary format. I did not choose to display the output in the binary format nor I chose to play with hex commands. The software version that is inside of the high voltage power supply is forcing me to communicate using these formats.

So all I am trying to do now is to display 76 00 02 as 7.6 E+02. I tried all kinds of conversions but none of them apply to my case.

I hope it is more clear now.

Thank you very much,
Pedja
0 Kudos
Message 3 of 29
(4,444 Views)
There are many ways to do this, here is a loop-free version that is relatively easy to read.
Message 4 of 29
(4,444 Views)
Coop wrote:

> I agree now that I am receiving the binary string at the output of the
> VISA Read.
>
> The string that I receive is in that format because the instrument
> sends it in the binary format. I did not choose to display the output
> in the binary format nor I chose to play with hex commands. The
> software version that is inside of the high voltage power supply is
> forcing me to communicate using these formats.

I know that and didn't say to try different. I also pointed out that the
example you gave me leaves much room for interpretation and I offered
you a VI diagram as attachment to convert the binary string into the
desired number in two different ways. They produce both the same result
for this particular example but are NOT e
quivalent.
Which one is right, if any, will have to be determined by further tests
and/or more detailed information from the manual. This example will at
least show you a way how you can start to work yourself on eventually
getting it right.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 29
(4,444 Views)
Thanks so much. This is one nice way of doing it and it is exactly what I needed.

Thanks again,
Pedja
0 Kudos
Message 6 of 29
(4,444 Views)
For a final solution, you should also add some error handling. For example, if the input contains illegal (non decimal) characters (e.g. 11 11 F0, 12 A2 02, etc. ) the result should be NaN.
One easy way would be to check the sum of the offsets. If it is not 6, it encountered a non-decimal character somewhere. See attached image. (The default case, just contains a NaN diagram constant wired to the output).
0 Kudos
Message 7 of 29
(4,444 Views)
I agree it is good to add the error handling in case something other than digits is read from the port.

One thing I have noticed is that the input is in this format 7600 02. I am not able to test my program with this example now because I am waiting for a longer serial port cable to arrive but I know that the format at the output of the VISA read is divided in three bytes 76 00 02.
From the manual: "The first two contain the mantissa in a packed BCD X.XXX format and the third byte contains the exponent in two's complement notation".

I will first add your part to my program and see how it goes.

Thanks for your help again,
Pedja
0 Kudos
Message 8 of 29
(4,444 Views)
Hi,

Just to tell you to ignore my previous comment.
Everything works perfectly.

Thanks again,
Pedja
0 Kudos
Message 9 of 29
(4,444 Views)
Hi guys,

I am writing you back because I tried the two solutions that you offered to me (with and without loop) and I think the problem still exists. This time I attached simpler example and you can see what I mean.

I am trying to read all the pressure levels at all 8 channels of my multi-gauge. Inside of the loop you will see the string constant that represents the pressure levels read from the VISA read.

Now that I have read all pressures I have to display all 8 gauges seperatelly so that's why I am using String Subset and storing data in 8 different indicators. This mechanism works well but the only problem that remains is how to display string eg. 1435 00 as 1.435E+00.

If I use the two examples that you suggested it will convert it prop
erly only if the input string is set to hex display and then the 1435 00 is typed in. In this case it does convert it right into 1.435E+00.

But the string that I receive from VISA read or the string constant that I use in this example can not be changed between normal and hex display. Therefore when 1435 00 enters the convert subVI it converts it to 3.134E+33.

What do I do guys?
I wasted a lot of time on this because I am forced to communicate in hex format and I am still facing the same problem.

Thanks for everything. I really appreciate it.

Regards,
Pedja
0 Kudos
Message 10 of 29
(4,444 Views)