LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Strings That Contain an IEEE-Format Real Number to a Real Variable

Solved!
Go to solution

Hello all,

 

I'm struggling with the soft trying to convert the measured value from a peripheral in format IEEE 32 bit float that I have in a unsigned char string[200] (bytes 56, 57, 58 and 59) into a double freq variable to show the value on a numeric control and save it on a csv file.

 

What I do is to get my 4 bytes in a separate unsigned char array unsigned char LatY[4] and then:

 

I have seen other related forums here but it doesn't work for me:

1)  Scan (LatY, "%1f[z]>%f", &freq); -> error trying to read beyond the array

2)  Fmt(freq, "%1f[z]", *(float*)LatY); -> no compatible format error

 

Can anyone please tell me if there is a direct way to do this? better than handling bit by bit in operations...

 

0 Kudos
Message 1 of 5
(3,453 Views)
Solution
Accepted by topic author josete

You must instruct Scan to read only 4 bytes out of the string: try with 

Scan (LatY, "%1f[zb4]>%f", &freq);


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 5
(3,438 Views)

Can't you do that directly with a cast since float is already IEEE-32 ?

float F=*(float*)LatY;

Warning: maybe you need to swap the bytes first.

0 Kudos
Message 3 of 5
(3,385 Views)

You're right: once the bytes have been extracted from the source string into LatY you can case the string to a float (or even a double). Byte swapping depends on whether instrument endianness matches PC one or not.

Scan can be more user friendly since with the appropriate use of parameters 'b' (argument lenght), 'i' (offset in the string) and 'o' (byte ordering) you could extract values without previously copying bytes to an intermediate string.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 5
(3,382 Views)

Extracting you say ?

float F=*(float*)&string[56];   Smiley Happy

0 Kudos
Message 5 of 5
(3,380 Views)