2

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

Input:

Hex: 0x3fe2da2f8bdec5f4
Hex: 0x402A000000000000

Output

Dec: .589134
Dec: 13
Wamadahama
  • 1,489
  • 13
  • 19

2 Answers2

2

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.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • This one is really clean! – Utkarsh Narain Srivastava Jun 11 '18 at 17:44
  • @UtkarshNarainSrivastava Note that I just tested this in a couple of node versions. In 10.4 I get the exact correct results, but in 9.4.0 I get some weird small errors. It seems like a bug in certain versions. EG. I get `13.000001912238076` for `402A000000000000` in node 9.4.0, even though that represents `13` exactly. – Paul Jun 11 '18 at 17:48
  • @UtkarshNarainSrivastava I found out it was a bug starting in exactly version 9.4.0 : https://github.com/nodejs/node/issues/18208 . It works in `9.3` and older but the bug exists in `9.4.0`, `9.5.0`, `9.6.0`, `9.6.1`, and `9.7`. It is fixed in `9.8` and later. If you are using this for personal use and not using those version you can use this, but if you are making an npm package or something that needs to be compatible with various node versions you should not use this. – Paul Jun 11 '18 at 17:55
  • Oh good news, there is a very easy fix to make it work in all versions of node. I will edit my answer. – Paul Jun 11 '18 at 17:57
  • @UtkarshNarainSrivastava Updated with a fix for all versions. Just make sure to specify the offset `0` as an argument to readDoubleBE and it will work in all versions. – Paul Jun 11 '18 at 18:00
  • thanks for the update. I will make the relevant changes. – Utkarsh Narain Srivastava Jun 12 '18 at 04:59
1

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));
Wamadahama
  • 1,489
  • 13
  • 19
  • DataView is undefined since this is a `node.js` question. – Paul Jun 11 '18 at 17:35
  • @UtkarshNarainSrivastava Not in node.js without a [library](https://www.npmjs.com/package/buffer-dataview). You tagged your question as `node.js`. – Paul Jun 11 '18 at 17:37
  • Updated my answer. Going to leave it here just in case someone is looking for a client side solution :-) – Wamadahama Jun 11 '18 at 17:41
  • No problem, your answer combined with the library I linked in the above comment works in node.js too. – Paul Jun 11 '18 at 17:41