64bit Hex to Decimal in Javascript

Utkarsh Narain Srivastava

Need to convert 64bit hex to decimal in node, preferably without 3rd party lib.

Input:

Hex: 0x3fe2da2f8bdec5f4
Hex: 0x402A000000000000

Output

Dec: .589134
Dec: 13
Paul

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related