Created
April 10, 2013 02:20
-
-
Save kkajita/5351241 to your computer and use it in GitHub Desktop.
格納先ポインタutf16には,十分な領域をあらかじめ確保しておく必要があります。
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
static int utf8to16(char* utf8, int utf8_bytes, wchar_t* utf16) | |
{ | |
int i = 0, j = 0; | |
while (i < utf8_bytes) { | |
char ch0 = utf8[i]; | |
if ((ch0 & 0x80) == 0x00) { | |
utf16[j++] = ch0 & 0x7F; | |
i += 1; | |
} else if ((ch0 & 0xE0) == 0xC0) { | |
utf16[j++] = ((ch0 & 0x1F) << 6) | (utf8[i+1] & 0x3F); | |
i += 2; | |
} else if ((ch0 & 0xF0) == 0xE0) { | |
utf16[j++] = ((ch0 & 0x0F) << 12) | ((utf8[i+1] & 0x3F) << 6) | (utf8[i+2] & 0x3F); | |
i += 3; | |
} else if ((ch0 & 0xF8) == 0xF0) { | |
int ucs = ((ch0 & 0x07) << 18) | ((utf8[i+1] & 0x3F) << 12) | | |
((utf8[i+2] & 0x3F) << 6) | (utf8[i+3] & 0x3F); | |
utf16[j++] = 0xD800 | (ucs >> 10); | |
utf16[j++] = 0xDC00 | (ucs & 0x03FF); | |
i += 4; | |
} else { | |
break; | |
} | |
} | |
utf16[j] = 0; | |
return j; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment