Last active
December 28, 2015 17:58
-
-
Save cuter44/7539217 to your computer and use it in GitHub Desktop.
jsString <=> UTF8 byte array convert
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
var utf8_scope = [0x0, 0x7f, 0x7ff, 0xffff, 0x10000]; | |
var utf8_prefix = [0x80, 0x00, 0xc0, 0xe0, 0xf0]; | |
var utf8_valid_bits = [6, 7, 5, 4, 3]; | |
var utf8_valid_mask = [0x3f, 0x7f, 0x1f, 0x0f, 0x07]; | |
function str2utf(str) | |
{ | |
//if (!(str instanceof String)) | |
//return; | |
var strbyte = new Array(); | |
for (var i=0; i<str.length; i++) | |
{ | |
var unicode = str.charCodeAt(i); | |
var k = 1; | |
while (unicode > utf8_scope[k]) | |
k++; | |
var utf8 = new Array(); | |
var j = k; | |
while (j>1) | |
{ | |
utf8.unshift(utf8_prefix[0] | (unicode & utf8_valid_mask[0])); | |
unicode = unicode >> 6; | |
j--; | |
} | |
utf8.unshift(utf8_prefix[k] | (unicode & utf8_valid_mask[k])) | |
strbyte = strbyte.concat(utf8); | |
} | |
return(strbyte); | |
} | |
function utf2str(utf) | |
{ | |
var strbyte = new Array(); | |
var cbyte = 0; | |
for (var i=0; i<utf.length; i++) | |
{ | |
var k = 0; | |
while ((utf[i]&~utf8_valid_mask[k]) != utf8_prefix[k]) | |
k++; | |
if (k != 0) | |
{ | |
strbyte.push(cbyte); | |
cbyte = 0; | |
} | |
cbyte = (cbyte << 6) | (utf[i] & utf8_valid_mask[k]); | |
} | |
strbyte.push(cbyte); | |
strbyte.shift(); | |
return(String.fromCharCode.apply(null, strbyte)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment