8

I have a float value representing GPS coordinates and I liked to convert it to a 32bit hex string.

I tried every solution described here but everytime, the result is not what I am expecting.

For example, most of the 'ToHex' functions :

var lat = 45.839152;
console.log(ToHex(lat));

returns me 2d.56d0b30b5aa8

but I am expecting 42355b43 for result as most converters returns

do you know how I could get 42355b43 as a result in JavaScript ?

Thank you !

5
  • For starters, JavaScript numbers are 64-bit values, not 32. Commented Nov 7, 2017 at 18:07
  • Ok but i need a 32bits hex value, i van truncate it i suppose ? Commented Nov 8, 2017 at 6:48
  • I made float to hex string and hex string to float converter functions, and I get 0x42375b4a for 45.839152. I get this same result using other folks functions as well, such as h-schmidt.net/FloatConverter/IEEE754.html and gregstoll.com/~gregstoll/floattohex If there's any interest I can place my javascript functions online, they are not obfuscated/minified like the other solutions so you can see how it works. Commented Aug 20, 2019 at 20:34
  • BTW the hex value you've provided translates to 45.3391227722168. Commented Aug 20, 2019 at 20:51
  • For decimal to float see also stackoverflow.com/q/57803/1066234 Commented Feb 17, 2023 at 12:56

2 Answers 2

12

You could take the TypedArray object with an ArrayBuffer and DataView.

Then set the value as float 32 and read the view as unsigned integer 8 bit for the values.

const getHex = i => ('00' + i.toString(16)).slice(-2);

var view = new DataView(new ArrayBuffer(4)),
    result;

view.setFloat32(0, 45.839152);

result = Array
    .apply(null, { length: 4 })
    .map((_, i) => getHex(view.getUint8(i)))
    .join('');

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

I finally decide to code my own function. I post it here so it could help people :

function ToHex(d) {

    var sign = "0";

    if(d<0.0){
        sign = "1";
        d = -d;
    }

    var mantissa = parseFloat(d).toString(2);

    var exponent = 0;

    if(mantissa.substr(0,1) === "0"){
        exponent = mantissa.indexOf('.') - mantissa.indexOf('1') + 127;
    }
    else{
        exponent = mantissa.indexOf('.') - 1 + 127;
    }

    mantissa = mantissa.replace(".", "");
    mantissa = mantissa.substr(mantissa.indexOf('1')+1);

    if(mantissa.length>23){
        mantissa = mantissa.substr(0,23);
    }
    else{
        while(mantissa.length<23){
            mantissa = mantissa +"0";
        }
    }

    var exp = parseFloat(exponent).toString(2);

    while(exp.length<8){
        exp = "0" + exp;
    }

    var numberFull = sign + exp + mantissa;

    return parseInt(numberFull, 2).toString(16);
}

1 Comment

If you pass it "0" it will return 3f800000 instead of zero, and if you pass 1999 it will return 3ef9e000 instead of 44f9e000, something doesnt seem quite right

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.