Created
July 31, 2020 16:11
-
-
Save Amourspirit/c959e13555a23fa7595e77b86a7664e0 to your computer and use it in GitHub Desktop.
Functions to convert string to UTF8 Array and Array to UTF8 String
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 stringToUTF8Array(str) { | |
let utf8 = []; | |
for (let i = 0; i < str.length; i++) { | |
let charcode = str.charCodeAt(i); | |
if (charcode < 0x80) utf8.push(charcode); | |
else if (charcode < 0x800) { | |
utf8.push(0xc0 | (charcode >> 6), | |
0x80 | (charcode & 0x3f)); | |
} | |
else if (charcode < 0xd800 || charcode >= 0xe000) { | |
utf8.push(0xe0 | (charcode >> 12), | |
0x80 | ((charcode >> 6) & 0x3f), | |
0x80 | (charcode & 0x3f)); | |
} | |
// surrogate pair | |
else { | |
i++; | |
// UTF-16 encodes 0x10000-0x10FFFF by | |
// subtracting 0x10000 and splitting the | |
// 20 bits of 0x0-0xFFFFF into two halves | |
charcode = 0x10000 + (((charcode & 0x3ff) << 10) | |
| (str.charCodeAt(i) & 0x3ff)); | |
utf8.push(0xf0 | (charcode >> 18), | |
0x80 | ((charcode >> 12) & 0x3f), | |
0x80 | ((charcode >> 6) & 0x3f), | |
0x80 | (charcode & 0x3f)); | |
} | |
} | |
return utf8; | |
} | |
function stringFromUTF8Array(data) { | |
const extraByteMap = [1, 1, 1, 1, 2, 2, 3, 0]; | |
var count = data.length; | |
var str = ""; | |
for (var index = 0; index < count;) { | |
var ch = data[index++]; | |
if (ch & 0x80) { | |
var extra = extraByteMap[(ch >> 3) & 0x07]; | |
if (!(ch & 0x40) || !extra || ((index + extra) > count)) | |
return null; | |
ch = ch & (0x3F >> extra); | |
for (; extra > 0; extra -= 1) { | |
var chx = data[index++]; | |
if ((chx & 0xC0) != 0x80) | |
return null; | |
ch = (ch << 6) | (chx & 0x3F); | |
} | |
} | |
str += String.fromCharCode(ch); | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment