Skip to content

Instantly share code, notes, and snippets.

@adelzhang
Created May 24, 2013 05:47
Show Gist options
  • Save adelzhang/5641507 to your computer and use it in GitHub Desktop.
Save adelzhang/5641507 to your computer and use it in GitHub Desktop.
UTF16toUTF8
static int UTF16len(unsigned short *utf16)
{
unsigned short * start = utf16;
while(*utf16++) ;
return (int)(utf16 - start - 1);
}
size_t __UTF16toUTF8(const unsigned short utf16char, char* utf8str) {
if (utf16char < 0x80) {
utf8str[0] = (utf16char >> 0 & 0x7F) | 0x00;
return 1;
} else if (utf16char < 0x0800) {
utf8str[0] = (utf16char >> 6 & 0x1F) | 0xC0;
utf8str[1] = (utf16char >> 0 & 0x3F) | 0x80;
return 2;
} else if (utf16char < 0x010000) {
utf8str[0] = (utf16char >> 12 & 0x0F) | 0xE0;
utf8str[1] = (utf16char >> 6 & 0x3F) | 0x80;
utf8str[2] = (utf16char >> 0 & 0x3F) | 0x80;
return 3;
} else if (utf16char < 0x110000) {
utf8str[0] = (utf16char >> 18 & 0x07) | 0xF0;
utf8str[1] = (utf16char >> 12 & 0x3F) | 0x80;
utf8str[2] = (utf16char >> 6 & 0x3F) | 0x80;
utf8str[3] = (utf16char >> 0 & 0x3F) | 0x80;
return 4;
}
return 0;
}
static void UTF16toUTF8(const short *src, char *dst){
int len = UTF16len(src);
unsigned short *end = src + len;
while (src < end){
dst += __UTF16toUTF8(*src,dst);
src++;
}
*dst = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment