Last active
December 18, 2015 06:10
-
-
Save tsmd/27d59ee494e41a7f0210 to your computer and use it in GitHub Desktop.
非負の数値の配列とBase64を相互変換
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 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
取り扱う数値が2バイトで収まるのなら
Uint32Array
をUint16Array
にすればよい。