conversion to single-precision float

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jcole1954
Posts: 2
Joined: Tue Jun 30, 2015 7:22 pm

conversion to single-precision float

Post by jcole1954 » Sat Sep 05, 2015 8:39 pm

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.

jcole1954
Posts: 2
Joined: Tue Jun 30, 2015 7:22 pm

Re: conversion to single-precision float

Post by jcole1954 » Fri Sep 11, 2015 6:45 pm

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

JonDZ
Posts: 2
Joined: Wed Nov 04, 2015 7:10 pm

Re: conversion to single-precision float

Post by JonDZ » Wed Nov 04, 2015 7:46 pm

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?

JonDZ
Posts: 2
Joined: Wed Nov 04, 2015 7:10 pm

Re: conversion to single-precision float

Post by JonDZ » Thu Nov 05, 2015 10:34 pm

Nevermind, must have made a mistake with your code. Got it working now, thanks!

Post Reply