Convert a string with a hex representation of an IEEE-754 double into JavaScript numeric variable -


Let me say I have a hex number "4072508200000000" and I want the floating point number that it shows in IEEE-754 (293.03173828125000). The double format is inserted into the JavaScript variable.

I can think in a way that calls for masking and pow (calls), but is this a simple solution?

A client-side solution is required.

This may be helpful, it is a website that allows you to enter the hex encoding of an IEEE-754 and get an analysis of Mentisa and Exponent.

Because people always ask "why?", Here's why: I am trying to fill the existing, but incomplete implementation of Google's Procol Buffers (protobuf).

I do not know a well, this can definitely be done in a difficult way, here's JavaScript There is a perfectly accurate example in:

  js> a = 0x41973333 1100428083 JS & gt; (A and 0x7fffff | 0x800000) * 1.0 / Mathipo (2,23) * Math.p. (2, (A &>; 23 & amp; 0xff) - 127) 18.899999618530273  

A production implementation should be kept in mind that most areas have magic values, which are usually implemented by specifying a specific explanation for being the largest or smallest. Therefore, NaN s and infinities Find out the above example should be checked for negative (another amp 0x80000000)

Update: OK, let me know You can not expand the above technique directly because the internal SS representation is a double, and with its definition it can handle a little bit of length 52, and it can handle more than 32 Can not go.

OK, to double you first cut as a string cut 8 digits or 32 bits; processed with a different object Then:

  JS> A = 0x40725082 1081233538 Sector S & gt; (e 0xfffff | 0x100000) * 1.0 / Mthipo (2, 52 - 32) * Mthkpo (2, (e ;; 52 - 32 & amp; 0x7ff) - 1023)) 293.03173828125 JSA & gt;  

I put the example above because it is from OP. A difficult case occurs when the value of less than 32 bits is true. Here is the conversion of 0x40725082deadbeef, a full-scale double:

  js> A = 0x40725082 1081233538 JS & gt; B = 0xdeadbeef 3735928559JS & gt; E = (A & gt; 52 - 32 & amp; 0x7ff) - 1023 8 JS & gt; (Another amp 0xfffff | 0x100000) * 1.0 / Mathipo (2,52-32) * Math.p. (2, E) + B * 1.0 / Mathipo (2, 52) * Math.p. (2, E) 293.031 9506442019JS & gt;  

Some obvious subexpressions can factor you, but I have left it this way so that you can see how it is related to the format.


Comments

Popular posts from this blog

MySql variables and php -

c# - How can I find out if there are windows above a control? -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -