Created
February 10, 2013 20:32
-
-
Save TooTallNate/4750953 to your computer and use it in GitHub Desktop.
Get host machine endianness using JavaScript Typed Arrays (polyfill for `os.endianness()` in node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function endianness () { | |
var b = new ArrayBuffer(4); | |
var a = new Uint32Array(b); | |
var c = new Uint8Array(b); | |
a[0] = 0xdeadbeef; | |
if (c[0] == 0xef) return 'LE'; | |
if (c[0] == 0xde) return 'BE'; | |
throw new Error('unknown endianness'); | |
} | |
endianness(); | |
// "LE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be careful with this one-line code - you can't blindly trust TypedArray.buffer property: see notes on https://nodejs.org/api/buffer.html#buffer_buf_buffer and https://nodejs.org/api/buffer.html#buffer_buf_byteoffset