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 !

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Aznhar
  • 610
  • 1
  • 10
  • 30
  • For starters, JavaScript numbers are 64-bit values, not 32. – Pointy Nov 07 '17 at 18:07
  • Ok but i need a 32bits hex value, i van truncate it i suppose ? – Aznhar Nov 08 '17 at 06: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 https://www.h-schmidt.net/FloatConverter/IEEE754.html and https://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. – user3015682 Aug 20 '19 at 20:34
  • BTW the hex value you've provided translates to 45.3391227722168. – user3015682 Aug 20 '19 at 20:51
  • For decimal to float see also https://stackoverflow.com/q/57803/1066234 – Avatar Feb 17 '23 at 12:56

2 Answers2

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
}
Aznhar
  • 610
  • 1
  • 10
  • 30
  • 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 – Anthony Webb May 16 '19 at 00:39