Skip to content

Instantly share code, notes, and snippets.

@mikkoh
Created June 4, 2015 14:28
Show Gist options
  • Save mikkoh/f8c5148871bb3ba57455 to your computer and use it in GitHub Desktop.
Save mikkoh/f8c5148871bb3ba57455 to your computer and use it in GitHub Desktop.
Converting between TypedArrays
/**
* To convert between typed arrays an ArrayBuffer can be used to create
* TypedArray's from one type to another. Each typed array has a property
* which is an ArrayBuffer.
**/
var uint16 = new Uint16Array([0x0102, 0x0304]);
var uint32 = new Uint32Array(uint16.buffer);
var uint8clamped = new Uint8ClampedArray(uint16.buffer);
var uint8 = new Uint8Array(uint16.buffer);
console.log('uint8', uint8); // uint8 [2, 1, 4, 3]
console.log('uint8clamped', uint8clamped); // [2, 1, 4, 3]
console.log('uint16', uint16); // [258, 772]
console.log('uint32', uint32); // [50594050]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment