从2进制文件读取一个UTF8字符串 ReadUTF8 ,跨平台

时间:2022-06-01 19:04:13

直接贴代码: 游戏开发群:44727718

 

如果缺少什么函数,请给下看。。

 

 1 string HSReadData::ReadUTF8()
 2 {
 3     
 4     int utflen = ReadShort() & 0xffff;
 5     wstring str;
 6     str.resize(utflen);
 7     vector<char> bytes;
 8     bytes.resize(utflen);
 9     for (int i=0;i<utflen;++i)
10     {
11         bytes[i] = this->ReadByte();
12     }
13 
14     int ch1, ch2, ch3;
15     int length = 0;
16     int index = 0;
17 
18     while(length < utflen)
19     {
20         ch1 = (int) bytes[length] & 0xff;
21         switch(ch1 >> 4)
22         {
23         case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
24             /* 0xxxxxxx*/
25             length++;
26             str[index++] = (wchar_t)ch1;
27             break;
28         case 12: case 13:
29             /* 110x xxxx   10xx xxxx*/
30             length += 2;
31             if(length > utflen)
32             {
33                 return "Error code -1";
34             }
35 
36             ch2 = (int)bytes[length - 1];
37             if((ch2 & 0xC0) != 0x80)
38             {
39                 return "Error code -1";
40             }
41 
42             str[index++] = (wchar_t)(((ch1 & 0x1F) << 6) | (ch2 & 0x3F));
43             break;
44         case 14:
45             /* 1110 xxxx  10xx xxxx  10xx xxxx */
46             length += 3;
47             if(length > utflen)
48             {
49                 return "Error code -1";
50             }
51 
52             ch2 = (int)bytes[length - 2];
53             ch3 = (int)bytes[length - 1];
54 
55             if(((ch2 & 0xC0) != 0x80) || ((ch3 & 0xC0) != 0x80))
56             {
57                 return "Error code -1";
58             }
59             str[index++] = (wchar_t)(((ch1 & 0x0F) << 12) | ((ch2 & 0x3F) << 6) | ((ch3 & 0x3F) << 0));
60             break;
61         default:
62             /* 10xx xxxx,  1111 xxxx */
63             return "Error code -1";
64         }
65     }
66     string text = HSTool::GetStringWithWstring(str);
67     
68     return text;
69 }

 

 

 1 short  HSReadData::ReadShort()
 2 {
 3     int b1 = Fgetc();
 4     int b2 = Fgetc();
 5     b1 &= 0xff;
 6     b2 &= 0xff;
 7     if (b1 == -1 || b2 == -1)
 8     {
 9         HSMessageBox("HSReadData @ ReadShort  throw -1 ");
10     }
11     int value = (b1 << 8) | (b2 << 0);
12     return (short) value;
13 }

 

 

 1 char  HSReadData::ReadByte()
 2 {
 3     int b1 = Fgetc();
 4     b1 &= 0xff;
 5     if (b1 == -1)
 6     {
 7         HSMessageBox("SPX3 @ ReadByte  throw -1 ");
 8     }
 9     int value = b1 & 0xff;
10     //CCLog("ReadByte : %d",b1);
11     return (char) value;
12 }

 1 std::string HSTool::GetStringWithWstring( const wstring& ws )
 2 {
 3 //     string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
 4 //     setlocale(LC_ALL, "chs"); 
 5 //     const wchar_t* _Source = ws.c_str();
 6 //     size_t _Dsize = 2 * ws.size() + 1;
 7 //     char *_Dest = new char[_Dsize];
 8 //     memset(_Dest,0,_Dsize);
 9 //     wcstombs(_Dest,_Source,_Dsize);
10 //     string result = _Dest;
11 //     delete []_Dest;
12 //     setlocale(LC_ALL, curLocale.c_str());
13     string ret;
14 
15     ret.resize(ws.size());
16 
17     for(size_t i = 0; i < ws.size(); ++i)
18     {
19         ret[i] = (string::value_type)ws[i];
20     }
21 
22     return ret;
23     return ret;
24 }

 

 1 wstring HSTool::GetWstringWithString( const std::string& s )
 2 {
 3 //     std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
 4 //     setlocale(LC_ALL, "chs"); 
 5 //     const char* _Source = s.c_str();
 6 //     size_t _Dsize = s.size() + 1;
 7 //     wchar_t *_Dest = new wchar_t[_Dsize];
 8 //     wmemset(_Dest, 0, _Dsize);
 9 //     mbstowcs(_Dest,_Source,_Dsize);
10 //     std::wstring result = _Dest;
11 //     delete []_Dest;
12 //     setlocale(LC_ALL, curLocale.c_str());
13     wstring ret;
14 
15     ret.resize(s.size());
16 
17     for(size_t i = 0; i < s.size(); ++i)
18     {
19         ret[i] = (wstring::value_type)s[i];
20     }
21 
22     return ret;
23 }