标签:
1 static int fetchWordFromUTF8(const chConstStringA& strText, WCHAR& result) 2 { 3 int nLength = strText.length(); 4 if(nLength <= 0) return 0; 5 6 LPCSTR lpszTextA = strText.c_ptr(); 7 BYTE byte = *lpszTextA; 8 if ((byte >> 7) == 0x00) 9 { 10 result = (WCHAR)(BYTE)*lpszTextA; 11 return 1; 12 } 13 else if ((byte >> 5) == 0x06) 14 { 15 if(nLength >= 2) 16 { 17 DWORD dw0 = (DWORD)(BYTE)lpszTextA[0]; 18 DWORD dw1 = (DWORD)(BYTE)lpszTextA[1]; 19 result = (WCHAR)((dw1&0x3F) | ((dw0&0x1F) << 6)); 20 return 2; 21 } 22 } 23 else if ((byte >> 4) == 0x0E) 24 { 25 if(nLength >= 3) 26 { 27 DWORD dw0 = (DWORD)(BYTE)lpszTextA[0]; 28 DWORD dw1 = (DWORD)(BYTE)lpszTextA[1]; 29 DWORD dw2 = (DWORD)(BYTE)lpszTextA[2]; 30 result = (WCHAR)((dw2&0x3F) | ((dw1&0x3F) << 6) | ((dw0&0x0F)<<12)); 31 return 3; 32 } 33 } 34 result = ‘?‘; 35 return 1; 36 } 37 38 static int fetchUTF8FromWord(const UINT& word, LPSTR szResult) 39 { 40 if (word < 0x0080) 41 { 42 szResult[0] = (ACHAR)word; 43 return 1; 44 } 45 else if (word < 0x0800) 46 { 47 szResult[0] = (ACHAR)(0xC0 | (BYTE)(word>>6)); 48 szResult[1] = (ACHAR)(0x80 | (BYTE)(word&0x003F)); 49 return 2; 50 } 51 else// if (word <= 0xFFFF) 52 { 53 szResult[0] = (ACHAR)(0xE0 | (BYTE)(word>>12)); 54 szResult[1] = (ACHAR)(0x80 | (BYTE)((word>>6) & 0x3F)); 55 szResult[2] = (ACHAR)(0x80 | (BYTE)(word&0x3F)); 56 return 3; 57 } 58 szResult[0] = ‘?‘; 59 return 1; 60 }
标签:
原文地址:http://www.cnblogs.com/hqu-ye/p/4502545.html