Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created January 19, 2013 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slaskis/0a1e7479ec9ddaaf2caf to your computer and use it in GitHub Desktop.
Save slaskis/0a1e7479ec9ddaaf2caf to your computer and use it in GitHub Desktop.
Node 0.9.7 endianness error
<!DOCTYPE html>
<html>
<head>
<title>Test Endian</title>
</head>
<body>
<script src='test-endian.js'></script>
</body>
</html>
var msg = new ArrayBuffer(4)
, dat = new DataView(msg);
dat.setUint8(0,1)
dat.setUint8(1,2)
dat.setUint8(2,3)
dat.setUint8(3,4)
// add msg seq & length
var cmd = new Uint8Array(3+msg.byteLength)
, dat = new DataView(cmd.buffer);
dat.setUint16(0,1);
dat.setUint8(2,msg.byteLength);
cmd.set(new Uint8Array(msg),3);
// build packet
var pkt = new Uint8Array(2+cmd.byteLength);
var dat = new DataView(pkt.buffer);
dat.setUint16(0,0)
pkt.set(cmd,2)
var dest = new DataView(pkt.buffer)
console.log('should equal 0:',dest.getUint16(0))
console.log('should equal 1:',dest.getUint16(2)) // fails on node 0.9.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment