跨平台Unicode与UTF8互转代码 (二)

2014-11-24 12:15:10 · 作者: · 浏览: 1
CharRequire > UTF8StringLength )
37: break;
38:
39: if ( OutUnicodeString )
40: {
41: wchar_t& WideChar = OutUnicodeString[UniIndex];
42: WideChar = (UTF8String[UTF8Index + 0] & 0x3F) << 6;
43: WideChar |= (UTF8String[UTF8Index + 1] & 0x3F);
44: }
45:
46: UTF8Index += cUTF8CharRequire;
47: }
48: else if((UTF8Char & 0xF0) == 0xE0) ///< 1110-xxxx 10xx-xxxx 10xx-xxxx
49: {
50: const uint32 cUTF8CharRequire = 3;
51:
52: // UTF8字码不足
53: if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
54: break;
55:
56: if ( OutUnicodeString )
57: {
58: wchar_t& WideChar = OutUnicodeString[UniIndex];
59:
60: WideChar = (UTF8String[UTF8Index + 0] & 0x1F) << 12;
61: WideChar |= (UTF8String[UTF8Index + 1] & 0x3F) << 6;
62: WideChar |= (UTF8String[UTF8Index + 2] & 0x3F);
63: }
64:
65:
66: UTF8Index += cUTF8CharRequire;
67: }
68: else if((UTF8Char & 0xF8) == 0xF0) ///< 1111-0xxx 10xx-xxxx 10xx-xxxx 10xx-xxxx
69: {
70: const uint32 cUTF8CharRequire = 4;
71:
72: // UTF8字码不足
73: if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
74: break;
75:
76: if ( OutUnicodeString )
77: {
78: wchar_t& WideChar = OutUnicodeString[UniIndex];
79:
80: WideChar = (UTF8String[UTF8Index + 0] & 0x0F) << 18;
81: WideChar = (UTF8String[UTF8Index + 1] & 0x3F) << 12;
82: WideChar |= (UTF8String[UTF8Index + 2] & 0x3F) << 6;
83: WideChar |= (UTF8String[UTF8Index + 3] & 0x3F);
84: }
85:
86: UTF8Index += cUTF8CharRequire;
87: }
88: else ///< 1111-10xx 10xx-xxxx 10xx-xxxx 10xx-xxxx 10xx-xxxx
89: {
90: const uint32 cUTF8CharRequire = 5;
91:
92: // UTF8字码不足
93: if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
94: break;
95:
96: if ( OutUnicodeString )
97: {
98: wchar_t& WideChar = OutUnicodeString[UniIndex];
99:
100: WideChar = (UTF8String[UTF8Index + 0] & 0x07) << 24;
101: WideChar = (UTF8String[UTF8Index + 1] & 0x3F) << 18;
102: WideChar = (UTF8String[UTF8Index + 2] & 0x3F) << 12;
103: WideChar |= (UTF8String[UTF8Index + 3] & 0x3F) << 6;
104: WideChar |= (UTF8String[UTF8Index + 4] & 0x3F);
105: }
106:
107: UTF8Index += cUTF8CharRequire;
108: }
109:
110:
111: UniIndex++;
112: }
113:
114: return UniIndex;
115: }
疗效: 用了此代码啊, 再也不用被iconv折磨了

摘自 战魂小筑