Created
August 3, 2015 11:59
-
-
Save hitman401/b248a0e9fecb7cf28280 to your computer and use it in GitHub Desktop.
Simple snippet to convert utf8 string to bytes and vice versa
This file contains hidden or 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 pack(bytes) { | |
var chars = []; | |
for(var i = 0, n = bytes.length; i < n;) { | |
chars.push(((bytes[i++] & 0xff) << 8) | (bytes[i++] & 0xff)); | |
} | |
return String.fromCharCode.apply(null, chars); | |
} | |
function unpack(str) { | |
var bytes = []; | |
for(var i = 0, n = str.length; i < n; i++) { | |
var char = str.charCodeAt(i); | |
bytes.push(char >>> 8, char & 0xFF); | |
} | |
return bytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment