Skip to content

Instantly share code, notes, and snippets.

@tsmd
Last active December 18, 2015 06:10
Show Gist options
  • Save tsmd/27d59ee494e41a7f0210 to your computer and use it in GitHub Desktop.
Save tsmd/27d59ee494e41a7f0210 to your computer and use it in GitHub Desktop.
非負の数値の配列とBase64を相互変換
function fromIntArray(array) {
var str = '';
var octet = new Uint8Array(new Uint32Array(array).buffer);
for (var i = 0; i < octet.length; i++) {
str += String.fromCharCode(octet[i]);
}
return btoa(str);
}
function toIntArray(ascii) {
var binary = atob(ascii);
var octet = new Uint8Array(binary.length);
for (var i = 0; i < binary.length; i += 1) {
octet[i] = binary.charCodeAt(i);
}
return new Uint32Array(octet.buffer);
}
@tsmd
Copy link
Author

tsmd commented Dec 18, 2015

取り扱う数値が2バイトで収まるのなら Uint32ArrayUint16Array にすればよい。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment