Need to convert 64bit hex to decimal in node, preferably without 3rd party lib.
Input:
Hex: 0x3fe2da2f8bdec5f4
Hex: 0x402A000000000000
Output
Dec: .589134
Dec: 13
Need to convert 64bit hex to decimal in node, preferably without 3rd party lib.
Input:
Hex: 0x3fe2da2f8bdec5f4
Hex: 0x402A000000000000
Output
Dec: .589134
Dec: 13
You can do this very easily in node.js without any libraries by using Buffer:
const hex = '3fe2da2f8bdec5f4';
const result = Buffer.from( hex, 'hex' ).readDoubleBE( 0 );
console.log( result );
WARNING: The offset of 0
is not optional. Several versions of the node.js API docs show examples of not supplying an offset for most Buffer functions and it being treated as an offset of 0
, but due to a bug in node.js versions 9.4.0
, 9.5.0
, 9.6.0
, 9.6.1
, and 9.7
you will get slightly incorrect results (EG. 13.000001912238076
instead of exactly 13
) if you do not specify an offset to readDoubleBE
in those versions.
For those trying to do this in client side javscript
// Split the array by bytes
a = "3fe2da2f8bdec5f4"
b = a.match(/.{2}/g);
// Create byte array
let buffer = new ArrayBuffer(8)
let bytes = new Uint8Array(buffer)
// Populate array
for(let i = 0; i < 8; ++i) {
bytes[i] = parseInt(b[i], 16);
}
// Convert and print
let view = new DataView(buffer)
console.log(view.getFloat64(0, false));