Skip to content

Instantly share code, notes, and snippets.

@malted
Created October 19, 2021 22:51
Show Gist options
  • Save malted/39afa64e22c567d5ad1bc067cc3a7412 to your computer and use it in GitHub Desktop.
Save malted/39afa64e22c567d5ad1bc067cc3a7412 to your computer and use it in GitHub Desktop.
function endian() {
const uInt16 = new Uint16Array([0x1122]);
const uInt8 = new Uint8Array(uInt16.buffer);
// If the first byte is 0x11, then we are on a big endian system, as they place the most significant byte first.
// (The most significant byte is the byte that has the greatest value.)
if (uInt8[0] === 0x11) console.log("Big endian system");
// On a big endian system, the most significant byte is first.
// So the first byte is 0x11 (0001 0001) (17)
else if (uInt8[0] === 0x22) console.log("Little endian system");
// On a little endian system, the most significant byte is last.
// So the first byte is 0x22 (0010 0010) (34)
// If you run the following code on a little endian system, the output would be
// 34
// 17
console.log(uInt8[0]);
console.log(uInt8[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment