使用DataView读取ArrayBuffer中的Int64

时间:2015-08-06 13:40:37

标签: javascript arraybuffer

我使用的是Web套接字。

我将ArrayBuffer传递给我的JavaScript页面。

我在C#代码中有这个:

 byte[] packet = new byte[2];
 packet[0] = (byte)1;
 packet[1] = (byte)0;

 byte[] tickArray = BitConverter.GetBytes( 635744635349556838 );
 byte[] packet2 = new byte[2 + tickArray.Length];
 Buffer.BlockCopy(packet, 0, packet2, 0, packet.Length);
 Buffer.BlockCopy(tickArray, 0, packet2, packet.Length, tickArray.Length);

在我的JavaScript客户端中,我有这个:

 var dv = new DataView(e.data);
 var marker = dv.getInt8(0);
 var tripped = dv.getInt8(1);

 var x = dv.getInt8(2);

我得到了结果:

1
0
-29

如果改为:

var x= dv.getInt16(1);

-7389

如何获得我的价值635744635349556838?

由于

1 个答案:

答案 0 :(得分:1)

我写了一个代码。会有用吗?

function getUint64(bytes, littleEndian)
{
  var low = 4, high = 0;
  if (littleEndian)
  {
    low = 0;
    high = 4;
  }

  var dv = new DataView(Uint8Array.from(bytes ).buffer);

  return (dv.getUint32(high, littleEndian) << 32) |
        dv.getUint32(low, littleEndian);
}
    var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
    var value = getUint64(bytes, false);
    console.log(value);