Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. static void UTF16_to_UTF8(u16 *stw, u8 *stb)
  2. {
  3.     while (SWAP16(stw[0])) {
  4.         if ((SWAP16(stw[0]) & 0xFF80) == 0) {
  5.             *(stb++) = SWAP16(stw[0]) & 0xFF;   // utf16 00000000 0xxxxxxx utf8 0xxxxxxx
  6.         }
  7.         else if ((SWAP16(stw[0]) & 0xF800) == 0) { // utf16 00000yyy yyxxxxxx utf8 110yyyyy 10xxxxxx
  8.             *(stb++) = ((SWAP16(stw[0]) >> 6) & 0xFF) | 0xC0; *(stb++) = (SWAP16(stw[0]) & 0x3F) | 0x80;
  9.         }
  10.         else if ((SWAP16(stw[0]) & 0xFC00) == 0xD800 && (SWAP16(stw[1]) & 0xFC00) == 0xDC00) { // utf16 110110ww wwzzzzyy 110111yy yyxxxxxx (wwww = uuuuu - 1)
  11.                                                                                                // utf8 1111000uu 10uuzzzz 10yyyyyy 10xxxxxx  
  12.             *(stb++) = (((SWAP16(stw[0]) + 64) >> 8) & 0x3) | 0xF0; *(stb++) = (((SWAP16(stw[0]) >> 2) + 16) & 0x3F) | 0x80;
  13.             *(stb++) = ((SWAP16(stw[0]) >> 4) & 0x30) | 0x80 | ((SWAP16(stw[1]) << 2) & 0xF); *(stb++) = (SWAP16(stw[1]) & 0x3F) | 0x80;
  14.             stw++;
  15.         }
  16.         else { // utf16 zzzzyyyy yyxxxxxx utf8 1110zzzz 10yyyyyy 10xxxxxx
  17.             *(stb++) = ((SWAP16(stw[0]) >> 12) & 0xF) | 0xE0; *(stb++) = ((SWAP16(stw[0]) >> 6) & 0x3F) | 0x80; *(stb++) = (SWAP16(stw[0]) & 0x3F) | 0x80;
  18.         }
  19.  
  20.         stw++;
  21.     }
  22.     *stb = 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement