Skip to content

Instantly share code, notes, and snippets.

@leandrodaher
Forked from TooTallNate/endianness.js
Created October 28, 2022 20:33
Show Gist options
  • Select an option

  • Save leandrodaher/a4d50c06559ea1a07e488f23ef0ac4a7 to your computer and use it in GitHub Desktop.

Select an option

Save leandrodaher/a4d50c06559ea1a07e488f23ef0ac4a7 to your computer and use it in GitHub Desktop.
Get host machine endianness using JavaScript Typed Arrays (polyfill for `os.endianness()` in node.js)
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