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 !
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With