Uint8Array(8)字符串化

时间:2015-09-26 15:25:15

标签: javascript stringify arraybuffer uint64

我从后端到ArrayBuffer得到无符号的64位整数,由于IEEE 754我无法得到&将此序列保持为数字。有没有办法“字符串化”这个庞大的数字?

简而言之:

var buffer = new ArrayBuffer(8);
var raw = new Uint8Array(buffer);

raw[0] = 255;
raw[1] = 255;
raw[2] = 255;
raw[3] = 255;
raw[4] = 255;
raw[5] = 255;
raw[6] = 255;
raw[7] = 255;

var view = new DataView(buffer);
var result = view.getUint64(0); /// :-(

提前谢谢。

2 个答案:

答案 0 :(得分:1)

是的,你可以对Int64进行字符串化,但没有"内置"这样做的方式就像你说的那样,JavaScript数字是双精度浮点数。

<?xml version='1.0' encoding='UTF-8'?>
<X12_00501_837_P xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <TS837_2000A_Loop>
        <NM1_SubLoop_2>
            <TS837_2010AA_Loop>
                <NM1_BillingProviderName>
                    <NM103_BillingProviderLastorOrganizationalName>VNA of Cape Cod</NM103_BillingProviderLastorOrganizationalName>
                    <NM109_BillingProviderIdentifier>1487651915</NM109_BillingProviderIdentifier>
                </NM1_BillingProviderName>
                <N3_BillingProviderAddress>
                  <N301_BillingProviderAddressLine>8669 NORTHWEST 36TH ST </N301_BillingProviderAddressLine>
                </N3_BillingProviderAddress>
            </TS837_2010AA_Loop>
        </NM1_SubLoop_2>
    </TS837_2000A_Loop>
</X12_00501_837_P>     

h2d来自this answer here

&#13;
&#13;
// ...
function readInt64(view){
    var small = view.getUint32(0).toString(16); // present as base 16 string
    var large = view.getUint32(1).toString(16);
    // in a power-of-2  base it's just a string concat to add them
    var bigNumber = large + small; 
    return h2d(bigNumber);
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

下面的DataView.getUint64()实现将为您提供一个尽可能精确的数字值:

How to Read 64-bit Integer from an ArrayBuffer / DataView in JavaScript