Hex or binary to float

Is there someone sitting on a conversion snippert that could perform the above on a 8 byte array ?

For example 32-bit hex 79 E9 F6 42 should convert to float 123.456

Little or big endian set properly of course.

I can do it using Processing but not been able to in Lua yet.

Re Peter

This probably isn’t what you want, but this is an easy way to convert regular hex to decimal.


function setup()
    print(tonumber("ab", 16))
    print(tonumber("abcd", 16))
    print(tonumber("abcdef", 16))
    print(tonumber("79E9F642",16))   
end

There’s a lua binary-decimal-hexadecimal library on the web that works in Codea. I put it in my library: http://www.math.ntnu.no/~stacey/code/CodeaLibrary

Here’s the routine. The conversion is dependent upon the precision of Codea which is only about 6 to 7 digits.

EDIT: changed code to one function.


-- convert hex to floating point.

function setup()
    tab={["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011",
         ["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111",
         ["8"]="1000",["9"]="1001",["a"]="1010",["b"]="1011",
         ["c"]="1100",["d"]="1101",["e"]="1110",["f"]="1111",
         ["A"]="1010",["B"]="1011",["C"]="1100",["D"]="1101",["E"]="1110",["F"]="1111"}
        
    print(hexToFloat("C58EBF11"))    -- -4567.89
    print(hexToFloat("42f6e979"))    -- 123.456
    print(hexToFloat("3f9e0610"))    -- 1.23456
end

function hexToFloat(str)
    local str1=""
    local a,z
    for z=1,string.len(str) do
        a=string.sub(str,z,z)
        str1=str1..tab[a]
    end
    local pm=string.sub(str1,1,1)
    local exp=string.sub(str1,2,9)
    local c=tonumber(exp,2)-127
    local p=math.pow(2,c)
    local man="1"..string.sub(str1,10,32)
    local x=0
    for z=1,string.len(man) do
        if string.sub(man,z,z)=="1" then
            x=x+p
        end
        p=p/2
    end
    if pm=="1" then
        x= -x
    end    
    return(x)
end

.@macflyerdk I’m writing a routine now. It kind of works so far I think, but I need to add code to look for error conditions and out of range numbers. I converted these 2 hex numbers. be580000 = .210938 44361000 = 728.25 . As for your 79e9f642 I get 1.5185e+35. Do you know what the value is supposed to be.

Nice work and thanks for taking the time dave1707 :slight_smile:
Sorry for my late reply, but Easter came in the way

First it did not work in my setup, but when I reversed the byte array to little endian format instead, all went well.

I have added this line to your function and one more parameter passed (endian)

if endian == "little" then -- big endian or little endian (reversed array)
str = string.sub(str,7,8)..string.sub(str,5,6)..string.sub(str,3,4)..string.sub(str,1,2) end

Do we have a place for code snippets like this ?
It can be a huge time and frustration saver, if these things could be collected and saved somewhere.

Re Peter

Edit: Nearly forgot. My number 79e9f642 was in the little endian format so reversing it to 42f6e979 results the number in my original post. Hence my added line to your function.

. @macflyerdk Thanks for the original post. I learned something about floating point hex and the code turned out easier than I thought it would. I never thought about little and big endian when I was doing the code. I looked on the Internet for the hex floating point format and went from there. One of my examples is 123.456 and I didn’t realize it was the reverse of what you had in your example. I like writing code for different things, it keeps me thinking.