LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Hex string to 32 bit float

Solved!
Go to solution

I am getting data over the serial port as a string of U8 bytes.

 

I convert the string to a sub string which reads 3B43 CD7B which corresponds to a value of 410.0 .( Or in other words my conversion should result in this value )

 

I tried to feed the above sub string to a typecast set for  double. But the value reads as  3.27607E-23.

 

Any hints ?

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 1 of 24
(5,083 Views)

Sorry, but something or someone (possibly me) is seriously confused.

To convert the string "3B43CD7B" into a U32 requires a SCAN FROM STRING with a "%x" (hex) specifier.

Outputs 1 and 2 in the pic show that this is done correctly. 

When you cast that to a SGL, you get 0.00298.

I don't know why you would cast it to a DBL, a 4-byte float is a SGL.

If you reverse the byte order, you get a huge (E+36) number, so that's not it. 

 

 

Hex.PNG

 

 

 

 

If you take the binary number (00111011010000111100110101111011)  apart, according to IEE 754 (standard SGL format), you get:

 

Sign = 0 = positive.

exponent = 01110110 (118) - bias (127) = -9

mantissa =  100001111.... = 271 / 512 = 0.529297

Accounting for the assumed MSB makes it 1.529297 

2 ^ -9 =  0.001953

1.529297 * 0.001953 = 0.002987  (I didn't carry all the digits).

 

So that conversion is correct.

  

I don't see how to get 410.0 from the data you gave us. 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

Message 2 of 24
(5,076 Views)

I convert the string to a sub string which reads 3B43 CD7B

 --- Perhaps an error occurs there.  Perhaps you're pulling the wrong pieces out.

 

Converting 410.0 to SGL results in a value of 43CD0000  Notice the 43CD similarity?

 

"3B" is ASCII code for ";" (semicolon).

 

Looks like you're not accounting for something in your string. 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

Message 3 of 24
(5,074 Views)

Hello Steve,

 

Thanks for the analysis. And trying to typecast a 32 bit to a double was a mistake, that happened over many iterations of different combinations !

 

Will collect a few more samples and try out based on the hints you gave. Shall revert shortly.

Message Edited by Raghunathan on 09-14-2009 09:26 PM
Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 4 of 24
(5,021 Views)
As mentioned your return string seems to start with ";" removing that and removing the space results in "43CD7b" which is wrong, but adding "00" to the end for a total of "43CD7b00" results in 410,961 when typecasted to single according to previous diagram. Could that be the problem? /Y
G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 5 of 24
(4,988 Views)
You will need to verify the format of the data that you will be reading and parse it correctly. As you can see from this example there is other information being returned from your device besides the raw data. What type of device are you communicating with? Do you have the spec for it? This would be a good place to start.
Message Edited by Mark Yedinak on 09-15-2009 08:43 AM


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 6 of 24
(4,982 Views)

Yes of course knowing the type of instrument is a good place to start. It is a EM6400 energy manager from Conzerv( now Schneider). I am attaching the data format details  it returns to a query.

 

I am also attaching the full U8 byte array returned by the instrument from where I am trying to "strip" the data value corresponding to 410.0 ( when I captured this is what the instrument was displaying ).

 

Thanks

Message Edited by Raghunathan on 09-15-2009 12:25 PM
Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
Download All
0 Kudos
Message 7 of 24
(4,958 Views)

It's hard to tell from that doc what you're doing.

 

How are you extracting the string to convert from the string it's supplied in?

 

It looks to me like you're extracting 4 bytes from an offset that's off by one.

 

IOW, you're starting at offset 10 when it should be 11, or something.

 

I suggest you try changing that procedure and seeing what happens. 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 8 of 24
(4,947 Views)
What is the query that you are sending to the device? From reading the manual, which is not very clear about communications with the device, it can return either a single data element or a block of registers. You should find out exactly what the query you are sending is returning. Perhaps you could send a different query that would return only the piece of information that you want. You may also want to contact the instrument manufacture directly to get better information regarding protocol for communicating with the device.


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 9 of 24
(4,930 Views)

Raghunathan,

 

Your current code reads two registers at unknown address ?  Modify your code to read 4 registers starting at the same address and post the resulting array of bytes.

0 Kudos
Message 10 of 24
(4,914 Views)