Converting a 64 bit HEX to Decimal Floating-Pointt in Javascript

Flavius

So basically I have a couple of numbers that come out as HEX values in the form of "3FF0000000000000" and I want to get float values of out these, pretty much like in here So in this particular case I'd expect "20.000000000000000" as a result - which I'll later trim to only 5 decimals, but that should be easy enough.

I've tried a couple of solutions but unfortunately I know too little about conversions (and javascript aswell) to know exactly what I might be doing wrong.

The latest try looks something like this:

const hexToFloat64 = (hex) => {
    var int32 = [],
        float64;
    if (hex.length > 4) {
        var high = hex.substr(0, 8),
            low = hex.substr(8, 8);
        int32.push(parseInt(high, 16), parseInt(low, 16));
    }

    var uint32 = new Uint32Array(int32);
    float64 = new Float64Array(uint32.buffer);
    var returnValue = float64[0];
    return returnValue;
};

Much obliged!

GetSet

This is NOT an answer to your exact problem. It IS a solution to decode the hex. Thats all i am doing here. I have no context to solve your problem.

function convHexStringToString(ss) {
// ss length must be even (or 0) when passed to this function
    var s = "";
    var p;
    if (ss.length > 0) {
        if (ss.length % 2 == 0) {
            
            var l = Math.floor(ss.length / 2); // floor must never have to do work
            
            for (var i = 0; i < l; i++) { 
                var i2 = i * 2;
                if (ss.charAt(i2) != "0") {
                    p = ss.charAt(i2) + ss.charAt((i2) + 1);
                }
                else {
                    p = ss.charAt((i2) + 1);
                }
                d = parseInt(p,16);
                s += String.fromCharCode(d);
            }
        }
    }
    return s;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related