Page 1 of 1
conversion to single-precision float
Posted: Sat Sep 05, 2015 8:39 pm
by jcole1954
I am trying to convert a HEX string into a REAL number. The string will ultimately come from plc in the form of an CANbus message but for now I just want to do the simple conversion to verify that I am getting the correct number in the output. If I use an online conversion tool that conforms to IEE-754 I get the following
decimal floating point 12.89 gives 4 byte 414E3D70
if I do the following in livecode
put binarydecode("f", "414E3D70", output1) into convertqty
put output1 into field "output1"
output1 displays 2883.075195
Does anyone know why I don't get the same conversion number? I would expect them to be the same.
Re: conversion to single-precision float
Posted: Fri Sep 11, 2015 6:45 pm
by jcole1954
It turns out that this is a three step process
1. change the byte order
2. encode
3 decode
here is a command structure. You pass it the Hex number and get back the decimal number according to the IEE standard. I did similar one for going the decimal to hex way
command HexToDecBigToLittle tInputHex
Global HexToDec
local bytenumber
put empty into tOutput1
put empty into tOutput2
put empty into HexToDec
repeat with bytenumber = 1 to 8
put byte bytenumber of tInputHex&tOutput1 into tOutput1
end repeat
put binaryencode("h*",tOutput1) into tOutput2
put binarydecode("f", tOutput2, HexToDec) into convertqty
end HexToDecBigToLittle
Re: conversion to single-precision float
Posted: Wed Nov 04, 2015 7:46 pm
by JonDZ
Hi jcole,
I'm experiencing a similar situaton, but can't get the results i need, even with your solution.
I'm reading OSC data from a server and already converted some results to hex numbers.
I have "41c80000" as hex number (converted from ascii; "AÈ") and it should convert to "25.0" in decimal numbers (according to the floating point converters online). I keep getting "0.000054" as a result with this code:
Code: Select all
put "41c80000" into tHexFps
put binarydecode("f*", tHexFps, HexToDec) into convertedHex
put HexToDec & CR after field "subtractOSC"
Could you or someone else reading this, assist me in this?
Re: conversion to single-precision float
Posted: Thu Nov 05, 2015 10:34 pm
by JonDZ
Nevermind, must have made a mistake with your code. Got it working now, thanks!