Skip to content

Instantly share code, notes, and snippets.

@tJener
Created July 26, 2012 18:08
Show Gist options
  • Save tJener/3183572 to your computer and use it in GitHub Desktop.
Save tJener/3183572 to your computer and use it in GitHub Desktop.
Network serialization
// Store number in Float64Array.
var arr = new Float64Array([ 15.290663048624992 ]);
// View the underlying ArrayBuffer as unsigned bytes.
var bytes = new Uint8Array( arr.buffer );
// Serialize to a string. Pay attention to endian in production code.
var str = [].map.call( bytes, function( byte ) {
return String.fromCharCode( byte );
}).join('');
// Or more bluntly put:
// var str = String.fromCharCode(
// bytes[0], bytes[1], bytes[2], bytes[3],
// bytes[4], bytes[5], bytes[6], bytes[7]
// );
str.length; // 8
var arr = new Float64Array([15.290663048624992]);
// { '0': 15.290663048624992,
// buffer:
// { '0': 0,
// '1': 0,
// '2': 128,
// '3': 201,
// '4': 209,
// '5': 148,
// '6': 46,
// '7': 64,
// byteLength: 8 },
// BYTES_PER_ELEMENT: 8,
// length: 1,
// set: [Function: set],
// slice: [Function: slice],
// byteOffset: 0,
// byteLength: 8,
// subarray: [Function: subarray] }
[15.290663048624992,2.0000000004989023,-24.90756910131313,0.12015604357058937,0.32514392007855847,-0.8798439564294107,0.32514392007855847]
"?¾Â‹ål6@.”Ñɀ\u0000\u0000?ÔÏ(qÎÂé@\u0000\u0000\u0000\u0000\u0011$d¿ì'®ƒNRzÀ8èVrÙ\u0000\u0000?ÔÏ(qÎÂé"
var transform = {
origin: {
x: 15.290663048624992,
y: 2.0000000004989023,
z: -24.90756910131313
},
rotation: {
w: 0.12015604357058937,
x: 0.32514392007855847,
y: -0.8798439564294107,
z: 0.32514392007855847
}
};
{"origin":{"x":15.290663048624992,"y":2.0000000004989023,"z":-24.90756910131313},"rotation":{"w":0.12015604357058937,"x":0.32514392007855847,"y":-0.8798439564294107,"z":0.32514392007855847}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment