LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert hex string to float (Big Endian)

Solved!
Go to solution

Hello dear forum,

 

I am currently struggling with a piece of code that is supposed to change the format of a hex string into a float of the type Mid-Big Endian.

The input is from a sensor and is displayed in Labview via a string indicator with hex display option enabled. The input consists of 4 bytes.

See my attached image.

 

I have been searching the forums and trying to manipulate the data in many different ways but i cannot get it to display the correct value.

I know that the hex string e.g. 3CBF 1442 should translate into -0.7347 float format. 

 

I have tried to re-construct the string to show "BF3C 4214" as the formats predicts but I cannot get any output close to the desired one.

 

Am I missing something obvious? Any help would be greatly appreciated and I would gladly eloborate of something is unclear.

0 Kudos
Message 1 of 12
(6,931 Views)

@simonhb wrote:

Hello dear forum,

 

I am currently struggling with a piece of code that is supposed to change the format of a hex string into a float of the type Mid-Big Endian.

The input is from a sensor and is displayed in Labview via a string indicator with hex display option enabled. The input consists of 4 bytes.

See my attached image.

 

I have been searching the forums and trying to manipulate the data in many different ways but i cannot get it to display the correct value.

I know that the hex string e.g. 3CBF 1442 should translate into -0.7347 float format. 

 

I have tried to re-construct the string to show "BF3C 4214" as the formats predicts but I cannot get any output close to the desired one.

 

Am I missing something obvious? Any help would be greatly appreciated and I would gladly eloborate of something is unclear.


First, I think you reversed the byte order in the first group of bytes.  If I reverse them to BF3C 1442, I can convert it to "-0.734684".  The file is a snippet saved in LV 2015.

I'm not sure you should try to download the image shown here, because I think that the forum will rob it of the "magic" necessary to convert it into LV code.

snippet.png

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 12
(6,921 Views)
Solution
Accepted by simonhb

You are doing some weird mixed-endian conversion where you only swap the strings for 1,2 and 3,4. This is most likely wrong. You should reverse the entire string if you think the byte order is wrong.

 

Bill is right, the string (in hex display) should be BF3C 1442 and interpreted as big endian. LabVIEW can translate that into a SGL in one step with the default settings. Here are two possibilities:

 

endian.png

 

0 Kudos
Message 3 of 12
(6,895 Views)

I only thought the string was incorrect because OP second example was what you would expect.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 12
(6,889 Views)

Thank you altenbach and billko,

 

It works perfectly. The problem just was that I was overthinking it after some long days at uni.

I used your method and got the right result (see attached snippet)

0 Kudos
Message 5 of 12
(6,852 Views)

@simonhb wrote:

Thank you altenbach and billko,

 

It works perfectly. The problem just was that I was overthinking it after some long days at uni.

I used your method and got the right result (see attached snippet)


Here is another way to do it. Don't know if it's any better or faster. But it is a little cleaner looking.

 

alternate.png

Message 6 of 12
(6,843 Views)

@jamiva wrote:

@simonhb wrote:

Thank you altenbach and billko,

 

It works perfectly. The problem just was that I was overthinking it after some long days at uni.

I used your method and got the right result (see attached snippet)


Here is another way to do it. Don't know if it's any better or faster. But it is a little cleaner looking.

 

alternate.png


It's pretty "Rube-ish" and not as flexible as the unflatten from string in that you cannot specify the endianness of the resulting number.  If you are going to use the typecast function, may as well use it only once and get it over with.

 

Edit:

Oh, I see you were trying to solve the original problem.  It was agreed that the original problem had the first word accidentally byte-swapped.  Your solution is fine if that is taken into account.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 7 of 12
(6,830 Views)

@simonhb wrote:

I used your method and got the right result (see attached snippet)


First of all, that's a plain image, not a snippet. (In LabVIEW, the term "snippet" has a very specific meaning and is an image that includes diagram code).

 

Second, your image mostly resembles your original code, nothing what Bill or Me suggested. Are you sure you attached the right image?

0 Kudos
Message 8 of 12
(6,824 Views)

That's better and works more in general.

 

I uploaded the wrong picture of my code. I do not concatenate the strings like before but make the conversion directly like your snippet shows billko.

 

Thank you for your help. 

0 Kudos
Message 9 of 12
(6,771 Views)

Rob28_0-1676126621054.png

I used the attached snippet to get the desired output. Hope it helps everyone.

0 Kudos
Message 10 of 12
(3,352 Views)