1

Let's say I have a hex value as a string:

var hexValue = "0x43480000";

How can I parse it in Node.js and convert it to a number (float)? Like:

var parsedNumber = hexStringToNumber(hexValue); // -> 200.0

Note that the hex value is meant to be a bit pattern for an IEEE-754 floating-point number. So the result is 200, not 1128792064 as it would be with just parseInt("0x43480000").

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Harmjan
  • 132
  • 12

2 Answers2

2

(The below obsoleted by the clarification by the OP.)

parseInt (MDN | spec) is perfectly happy to parse hex in that format for you:

var hexValue = parseInt("0x43480000");
console.log(hexValue);

It will see the 0x at the beginning and assume hex. If you had a string in hex without the prefix ("43480000"), you'd use parseInt(str, 16) to tell parseInt to use base 16 (hex). But since you have the prefix, there's no need.

The result will be a JavaScript number, which by definition is a "double" (IEEE-754 double-precision binary floating point).


You've said you want 0x43480000 to come out as 200.0. That means you're not really parsing hex, you're trying to take a string containing the bit pattern of an IEEE-754 floating-point number, expressed as hex, and get the equivalent IEEE-754 floating-point number. That's very different from "parsing hex."

The calculator you referred to works with IEEE-754 single-precision binary floating point values. The 0x43480000 bit pattern isn't 200 in double-precision binary floating point. But 0x40690000 is.

The only easy way I know of to do that is with DataView:

var dv = new DataView(new ArrayBuffer(8));
dv.setUint32(0, parseInt("0x40690000"));
var num = dv.getFloat64(0)
console.log(num);

If you want to convert the single precision version (0x43480000), I don't think there's anything built-in that's going to do it for you. You'll have to isolate the mantissa and exponent bits, do the widening, and then do the above.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • But that doesn't make it a float or double. – Harmjan Aug 02 '16 at 15:30
  • @HarmjanKors: Yes it does. All numbers in JavaScript are doubles (IEEE-754 double-precision binary floating point). I would have said, but I thought you knew that from the way you phrased your question. :-) – T.J. Crowder Aug 02 '16 at 15:31
  • Yes I know :) But `parseInt("0x43480000")` results in `1128792064`, but I would like to receive value `200.0` (like this converter for example: http://www.h-schmidt.net/FloatConverter/IEEE754.html). – Harmjan Aug 02 '16 at 15:37
  • @HarmjanKors: That calculator works with IEEE-754 **single**-precision binary floating point values. That bit pattern isn't 200 in **double**-precision binary floating point, but 0x40690000 is. I've added an example of converting that in the above. – T.J. Crowder Aug 02 '16 at 15:53
2

The Node.js Buffer class provides this feature:

var buf = new Buffer("43480000", "hex");
var number = buf.readFloatBE(0);
console.log(number); // 200

Also see:

Community
  • 1
  • 1
Dominik
  • 151
  • 6
  • Nicely done, but really what you've done is establish that this question is a duplicate of your first link. You can't comment yet (you will be able to soon at >= 50 rep), but when you can the thing to do is to post a link to the original question as a comment on the duplicate. – T.J. Crowder Aug 02 '16 at 16:01