You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
663 B
25 lines
663 B
|
3 years ago
|
/**
|
||
|
|
* @description: utf16to8
|
||
|
|
* @param {String} str
|
||
|
|
* @return {String}
|
||
|
|
*/
|
||
|
|
export default function utf16to8(str) {
|
||
|
|
var out, i, len, c;
|
||
|
|
out = '';
|
||
|
|
len = str.length;
|
||
|
|
for (i = 0; i < len; i++) {
|
||
|
|
c = str.charCodeAt(i);
|
||
|
|
if (c >= 0x0001 && c <= 0x007f) {
|
||
|
|
out += str.charAt(i);
|
||
|
|
} else if (c > 0x07ff) {
|
||
|
|
out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f));
|
||
|
|
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f));
|
||
|
|
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
|
||
|
|
} else {
|
||
|
|
out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f));
|
||
|
|
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|