Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Created July 21, 2011 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaniel/1096982 to your computer and use it in GitHub Desktop.
Save tsaniel/1096982 to your computer and use it in GitHub Desktop.
Unicode(UTF-16) to UTF-8
function(text) {
return text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\u0000-\uD7FF\uE000-\uFFFF]/g, function($, offset) {
var isDoubleByte = $.length === 2
var unicode = isDoubleByte ?
(($.charCodeAt(offset) & 1023) << 10 | ($.charCodeAt(offset + 1) & 1023)) + 0x10000:
$.charCodeAt(offset);
return isDoubleByte ?
String.fromCharCode(
unicode >> 18 | 240,
unicode >> 12 & 63 | 128,
unicode >> 6 & 63 | 128,
unicode & 63 | 128
) :
unicode < 128 ?
String.fromCharCode(
unicode
) :
unicode < 2048 ?
String.fromCharCode(
unicode >> 6 | 192,
unicode & 63 | 128
) :
String.fromCharCode(
unicode >> 12 | 224,
unicode >> 6 & 63 | 128,
unicode & 63 | 128
);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment