国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

[分享]解決C++ client socket send to Workerman 中文亂碼問題

egirlasm

在windows上 用正常的socket
[code]send(s,"你好世界",4,0);[/code]
這樣workerman服務器收到的始終是亂碼.這是因為workerman里使用的都是 utf-8

因此發(fā)送之前,一定要。[code]string UnicodeToUTF8(const wstring& str)
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte(936,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL);
pElementText = new char[iTextLen + 1];
memset((void[i])pElementText, 0, sizeof(char) [/i] (iTextLen + 1));
::WideCharToMultiByte(CP_UTF8,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL);
string strText;
strText = pElementText;
delete pElementText;
return strText;
}

wstring str = L"你好世界";
string utf8str = UnicodeToUTF8(str);
send(s,utf8str.cstr(),utf8str.length(),0);
[/code]上面的會被截斷一部分文字,故把字節(jié)buffer弄大了一點,就沒問題了。
修正如下,[code]string UnicodeToUTF8(const wstring& str)
{
char pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte(936,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL);
pElementText = new char[iTextLen
2 + 1];
memset((void[i])pElementText, 0, sizeof(char) [/i] (iTextLen2 + 1));
::WideCharToMultiByte(CP_UTF8,
0,
str.c_str(),
-1,
pElementText,
iTextLen
2,
NULL,
NULL);
string strText;
strText = pElementText;
delete pElementText;
return strText;
}[/code]

4842 1 0
1個評論

walkor

感謝@egirlasm的分享

  • 暫無評論
年代過于久遠,無法發(fā)表評論

egirlasm

2030
積分
0
獲贊數(shù)
0
粉絲數(shù)
2016-11-17 加入
??