01-23-2017 02:43 AM
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...
Solved! Go to Solution.
01-23-2017 03:37 AM
You must instruct Scan to read only 4 bytes out of the string: try with
Scan (LatY, "%1f[zb4]>%f", &freq);
01-25-2017 07:25 AM
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.
01-25-2017 09:19 AM
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.
01-25-2017 10:02 AM
Extracting you say ?
float F=*(float*)&string[56];