{"id":22,"date":"2023-05-19T20:19:35","date_gmt":"2023-05-19T12:19:35","guid":{"rendered":"http:\/\/www.greatfar.cn\/?p=22"},"modified":"2023-05-19T20:19:35","modified_gmt":"2023-05-19T12:19:35","slug":"%e5%ad%97%e7%ac%a6%e9%9b%86%ef%bc%9a%e5%a4%9a%e5%ad%97%e8%8a%82%e7%bc%96%e7%a0%81-%e4%b8%8e-unicode%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.greatfar.cn\/archives\/22","title":{"rendered":"\u5b57\u7b26\u96c6\uff1a\u591a\u5b57\u8282\u7f16\u7801 \u4e0e Unicode\u7801"},"content":{"rendered":"\n

<\/strong><\/strong>\u8f6c\uff1a \u5e26\u4f60\u73a9\u8f6cVisual Studio\u2014\u2014\u5e26\u4f60\u7406\u89e3\u591a\u5b57\u8282\u7f16\u7801\u4e0eUnicode\u7801<\/a><\/strong><\/p>\n\n\n\n

\u591a\u5b57\u8282\u5b57\u7b26\u4e0e\u5bbd\u5b57\u8282\u5b57\u7b26<\/strong><\/p>\n\n\n\n

<\/a>char \u4e0e wchar_t<\/strong><\/p>\n\n\n\n

\u6211\u4eec\u77e5\u9053C++\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e2d\u8868\u793a\u5b57\u7b26\u7684\u6709\u4e24\u79cd\uff1achar\u3001wchar_t\u3002 <\/p>\n\n\n\n

char\u53eb\u591a\u5b57\u8282\u5b57\u7b26<\/strong>\uff0c\u4e00\u4e2achar\u5360\u4e00\u4e2a\u5b57\u8282\uff0c\u4e4b\u6240\u4ee5<\/strong>\u53eb\u591a\u5b57\u8282\u5b57\u7b26\u662f\u56e0\u4e3a\u5b83\u8868\u793a\u4e00\u4e2a\u5b57<\/strong>\u65f6\u53ef\u80fd\u662f\u4e00\u4e2a\u5b57\u8282\u4e5f\u53ef\u80fd\u662f\u591a\u4e2a\u5b57\u8282\u3002\u4e00\u4e2a\u82f1\u6587\u5b57\u7b26(\u5982\u2019s\u2019)\u7528\u4e00\u4e2achar(\u4e00\u4e2a\u5b57\u8282)\u8868\u793a\uff0c\u4e00\u4e2a\u4e2d\u6587\u6c49\u5b57(\u5982\u2019\u4e2d\u2019)\u75283\u4e2achar(\u4e09\u4e2a\u5b57\u8282)\u8868\u793a\uff0c\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n

void TestChar() { char ch1 = ‘s’; \/\/ \u6b63\u786e cout << “ch1:” << ch1 << endl; char ch2 = ‘\u4e2d’; \/\/ \u9519\u8bef,\u4e00\u4e2achar\u4e0d\u80fd\u5b8c\u6574\u5b58\u653e\u4e00\u4e2a\u6c49\u5b57\u4fe1\u606f cout << “ch2:” << ch2 << endl; char str[4] = “\u4e2d”; \/\/\u524d\u4e09\u4e2a\u5b57\u8282\u5b58\u653e\u6c49\u5b57’\u4e2d’,\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u5b58\u653e\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26\\0 cout << “str:” << str << endl; \/\/char str2[2] = “\u56fd”; \/\/ \u9519\u8bef\uff1a’str2′ : array bounds overflow \/\/cout << str2 << endl; }<\/p>\n\n\n\n

\u7ed3\u70b9\u5982\u4e0b\uff1a<\/p>\n\n\n\n

ch1:s ch2: str:\u4e2d<\/p>\n\n\n\n

wchar_t\u88ab\u79f0\u4e3a\u5bbd\u5b57\u7b26<\/strong>\uff0c\u4e00\u4e2awchar_t\u53602\u4e2a\u5b57\u8282\u3002\u4e4b\u6240\u4ee5\u53eb\u5bbd\u5b57\u7b26\u662f\u56e0\u4e3a\u6240\u6709\u7684\u5b57\u90fd\u8981\u7528\u4e24\u4e2a\u5b57\u8282(\u5373\u4e00\u4e2awchar_t)\u6765\u8868\u793a\uff0c\u4e0d\u7ba1\u662f\u82f1\u6587\u8fd8\u662f\u4e2d\u6587<\/strong>\u3002\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n

void TestWchar_t() { wcout.imbue(locale(“chs”)); \/\/ \u5c06wcout\u7684\u672c\u5730\u5316\u8bed\u8a00\u8bbe\u7f6e\u4e3a\u4e2d\u6587 wchar_t wch1 = L’s’; \/\/ \u6b63\u786e wcout << “wch1:” << wch1 << endl; wchar_t wch2 = L’\u4e2d’; \/\/ \u6b63\u786e,\u4e00\u4e2a\u6c49\u5b57\u7528\u4e00\u4e2awchar_t\u8868\u793a wcout << “wch2:” << wch2 << endl; wchar_t wstr[2] = L”\u4e2d”; \/\/ \u524d\u4e24\u4e2a\u5b57\u8282(\u524d\u4e00\u4e2awchar_t)\u5b58\u653e\u6c49\u5b57’\u4e2d’,\u6700\u540e\u4e24\u4e2a\u5b57\u8282(\u540e\u4e00\u4e2awchar_t)\u5b58\u653e\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26\\0 wcout << “wstr:” << wstr << endl; wchar_t wstr2[3] = L”\u4e2d\u56fd”; wcout << “wstr2:” << wstr2 << endl; }<\/p>\n\n\n\n

\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n

ch1:s ch2:\u4e2d str:\u4e2d str2:\u4e2d\u56fd<\/p>\n\n\n\n

\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n

1. \u7528\u5e38\u91cf\u5b57\u7b26\u7ed9wchar_t\u53d8\u91cf\u8d4b\u503c\u65f6\uff0c\u524d\u9762\u8981\u52a0L \u5982\uff1a wchar_t wch2 = L\u2019\u4e2d\u2019; <\/p>\n\n\n\n

2. \u7528\u5e38\u91cf\u5b57\u7b26\u4e32\u7ed9wchar_t\u6570\u7ec4\u8d4b\u503c\u65f6,\u524d\u9762\u8981\u52a0L\u3002\u5982\uff1a wchar_t wstr2[3] = L\u201d\u4e2d\u56fd\u201d; <\/p>\n\n\n\n

3. \u5982\u679c\u4e0d\u52a0L\uff0c\u5bf9\u4e8e\u82f1\u6587\u53ef\u4ee5\u6b63\u5e38\uff0c\u4f46\u5bf9\u4e8e\u975e\u82f1\u6587(\u5982\u4e2d\u6587)\u4f1a\u51fa\u9519\u3002<\/strong><\/p>\n\n\n\n

string\u4e0ewstring<\/strong><\/p>\n\n\n\n

\u5b57\u7b26\u6570\u7ec4\u53ef\u4ee5\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u662f\u4e00\u4e2a\u5b9a\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5728\u4f7f\u7528\u4e4b\u524d\u5fc5\u987b\u77e5\u9053\u8fd9\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4e3a\u65b9\u4fbf\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\uff0cSTL\u4e3a\u6211\u4eec\u5b9a\u4e49\u597d\u4e86\u5b57\u7b26\u4e32\u7684\u7c7bstring\u548cwstring\u3002\u5927\u5bb6\u5bf9string\u80af\u5b9a\u4e0d\u964c\u751f\uff0c\u4f46wstring\u53ef\u80fd\u5c31\u7528\u7684\u5c11\u4e86\u3002<\/p>\n\n\n\n

string\u662f\u666e\u901a\u7684\u591a\u5b57\u8282\u7248\u672c\uff0c\u662f\u57fa\u4e8echar\u7684\uff0c\u5bf9char\u6570\u7ec4\u8fdb\u884c\u7684\u4e00\u79cd\u5c01\u88c5\u3002<\/p>\n\n\n\n

wstring\u662fUnicode\u7248\u672c\uff0c\u662f\u57fa\u4e8ewchar_t\u7684\uff0c\u5bf9wchar_t\u6570\u7ec4\u8fdb\u884c\u7684\u4e00\u79cd\u5c01\u88c5\u3002<\/p>\n\n\n\n

<\/a>string \u4e0e wstring\u7684\u76f8\u5173\u8f6c\u6362\uff1a<\/strong><\/p>\n\n\n\n

\u4ee5\u4e0b\u7684\u4e24\u4e2a\u65b9\u6cd5\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u53ef\u5728Windows\u4e0b\u4f7f\u7528\uff0c\u4e5f\u53ef\u5728Linux\u4e0b\u4f7f\u7528\u3002<\/p>\n\n\n\n

#include <cstdlib> #include <string.h> #include <string> \/\/ wstring => string std::string WString2String(const std::wstring& ws) { std::string strLocale = setlocale(LC_ALL, “”); const wchar_t* wchSrc = ws.c_str(); size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1; char *chDest = new char[nDestSize]; memset(chDest,0,nDestSize); wcstombs(chDest,wchSrc,nDestSize); std::string strResult = chDest; delete []chDest; setlocale(LC_ALL, strLocale.c_str()); return strResult; } \/\/ string => wstring std::wstring String2WString(const std::string& s) { std::string strLocale = setlocale(LC_ALL, “”); const char* chSrc = s.c_str(); size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1; wchar_t* wchDest = new wchar_t[nDestSize]; wmemset(wchDest, 0, nDestSize); mbstowcs(wchDest,chSrc,nDestSize); std::wstring wstrResult = wchDest; delete []wchDest; setlocale(LC_ALL, strLocale.c_str()); return wstrResult; }<\/p>\n\n\n\n

\u5b57\u7b26\u96c6(Charcater Set)\u4e0e\u5b57\u7b26\u7f16\u7801(Encoding)<\/strong><\/p>\n\n\n\n

<\/a>\u5b57\u7b26\u96c6\uff08Charcater Set\u6216Charset\uff09\uff1a<\/strong>\u662f\u4e00\u4e2a\u7cfb\u7edf\u652f\u6301\u7684\u6240\u6709\u62bd\u8c61\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u4e5f\u5c31\u662f\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u96c6\u5408\u3002\u5b57\u7b26\u662f\u5404\u79cd\u6587\u5b57\u548c\u7b26\u53f7\u7684\u603b\u79f0\uff0c\u5305\u62ec\u5404\u56fd\u5bb6\u6587\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u3001\u56fe\u5f62\u7b26\u53f7\u3001\u6570\u5b57\u7b49\u3002\u5e38\u89c1\u7684\u5b57\u7b26\u96c6\u6709\uff1aASCII\u5b57\u7b26\u96c6\u3001GB2312\u5b57\u7b26\u96c6(\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u4e2d\u6587\u6c49\u5b57)\u3001GBK\u5b57\u7b26\u96c6(\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u4e2d\u6587\u6c49\u5b57)\u3001Unicode\u5b57\u7b26\u96c6\u7b49\u3002<\/p>\n\n\n\n

\u5b57\u7b26\u7f16\u7801\uff08Character Encoding\uff09\uff1a<\/strong>\u662f\u4e00\u5957\u6cd5\u5219\uff0c\u4f7f\u7528\u8be5\u6cd5\u5219\u80fd\u591f\u5bf9\u81ea\u7136\u8bed\u8a00\u7684\u5b57\u7b26\u7684\u4e00\u4e2a\u5b57\u7b26\u96c6\uff08\u5982\u5b57\u6bcd\u8868\u6216\u97f3\u8282\u8868\uff09\uff0c\u4e0e\u8ba1\u7b97\u673a\u80fd\u8bc6\u522b\u7684\u4e8c\u8fdb\u5236\u6570\u5b57\u8fdb\u884c\u914d\u5bf9\u3002\u5373\u5b83\u80fd\u5728\u7b26\u53f7\u96c6\u5408\u4e0e\u6570\u5b57\u7cfb\u7edf\u4e4b\u95f4\u5efa\u7acb\u5bf9\u5e94\u5173\u7cfb\uff0c\u662f\u4fe1\u606f\u5904\u7406\u7684\u4e00\u9879\u57fa\u672c\u6280\u672f\u3002\u901a\u5e38\u4eba\u4eec\u7528\u7b26\u53f7\u96c6\u5408\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\u5c31\u662f\u6587\u5b57\uff09\u6765\u8868\u8fbe\u4fe1\u606f\uff0c\u800c\u8ba1\u7b97\u673a\u7684\u4fe1\u606f\u5904\u7406\u7cfb\u7edf\u5219\u662f\u4ee5\u4e8c\u8fdb\u5236\u7684\u6570\u5b57\u6765\u5b58\u50a8\u548c\u5904\u7406\u4fe1\u606f\u7684\u3002\u5b57\u7b26\u7f16\u7801\u5c31\u662f\u5c06\u7b26\u53f7\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u80fd\u8bc6\u522b\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\u3002<\/p>\n\n\n\n

\u4e00\u822c\u4e00\u4e2a\u5b57\u7b26\u96c6\u7b49\u540c\u4e8e\u4e00\u4e2a\u7f16\u7801\u65b9\u5f0f\uff0cANSI\u4f53\u7cfb(ANSI\u662f\u4e00\u79cd\u5b57\u7b26\u4ee3\u7801\uff0c\u4e3a\u4f7f\u8ba1\u7b97\u673a\u652f\u6301\u66f4\u591a\u8bed\u8a00\uff0c\u901a\u5e38\u4f7f\u7528 0x80~0xFF \u8303\u56f4\u7684 2 \u4e2a\u5b57\u8282\u6765\u8868\u793a 1 \u4e2a\u5b57\u7b26)\u7684\u5b57\u7b26\u96c6\u5982ASCII\u3001ISO 8859-1\u3001BIG5\u3001GB2312\u3001JIS\u3001GBK\u7b49\u7b49\u3002\u4e00\u822c\u6211\u4eec\u8bf4\u4e00\u79cd\u7f16\u7801\u90fd\u662f\u9488\u5bf9\u67d0\u4e00\u7279\u5b9a\u7684\u5b57\u7b26\u96c6\u3002 <\/p>\n\n\n\n

\u4e00\u4e2a\u5b57\u7b26\u96c6\u4e0a\u4e5f\u53ef\u4ee5\u6709\u591a\u79cd\u7f16\u7801\u65b9\u5f0f\uff0c\u4f8b\u5982UCS\u5b57\u7b26\u96c6(\u4e5f\u662fUnicode\u4f7f\u7528\u7684\u5b57\u7b26\u96c6)\u4e0a\u6709UTF-8\u3001UTF-16\u3001UTF-32\u7b49\u7f16\u7801\u65b9\u5f0f\u3002<\/p>\n\n\n\n

\u4ece\u8ba1\u7b97\u673a\u5b57\u7b26\u7f16\u7801\u7684\u53d1\u5c55\u5386\u53f2\u89d2\u5ea6\u6765\u770b\uff0c\u5927\u6982\u7ecf\u5386\u4e86\u4e09\u4e2a\u9636\u6bb5\uff1a <\/p>\n\n\n\n

\u7b2c\u4e00\u4e2a\u9636\u6bb5<\/strong>\uff1aASCII\u5b57\u7b26\u96c6 \u548c ASCII\u7f16\u7801<\/p>\n\n\n\n

\u8ba1\u7b97\u673a\u521a\u5f00\u59cb\u53ea\u652f\u6301\u82f1\u8bed(\u5373\u62c9\u4e01\u5b57\u7b26)\uff0c\u5176\u5b83\u8bed\u8a00\u4e0d\u80fd\u591f\u5728\u8ba1\u7b97\u673a\u4e0a\u5b58\u50a8\u548c\u663e\u793a\u3002ASCII\u7528\u4e00\u4e2a\u5b57\u8282(Byte)\u76847\u4f4d(bit)\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u7b2c\u4e00\u4f4d\u7f6e0\u3002\u540e\u6765\u4e3a\u4e86\u8868\u793a\u66f4\u591a\u7684\u6b27\u6d32\u5e38\u7528\u5b57\u7b26\u53c8\u5bf9ASCII\u8fdb\u884c\u4e86\u6269\u5c55\uff0c\u53c8\u6709\u4e86EASCII\uff0cEASCII\u75288\u4f4d\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f7f\u5b83\u80fd\u591a\u8868\u793a128\u4e2a\u5b57\u7b26\uff0c\u652f\u6301\u4e86\u90e8\u5206\u897f\u6b27\u5b57\u7b26\u3002<\/p>\n\n\n\n

\u7b2c\u4e8c\u4e2a\u9636\u6bb5<\/strong>: ANSI<\/strong>\u7f16\u7801\uff08\u672c\u5730\u5316\uff09 <\/p>\n\n\n\n

\u4e3a\u4f7f\u8ba1\u7b97\u673a\u652f\u6301\u66f4\u591a\u8bed\u8a00\uff0c\u901a\u5e38\u4f7f\u7528 0x80~0xFF \u8303\u56f4\u7684 2 \u4e2a\u5b57\u8282\u6765\u8868\u793a 1 \u4e2a\u5b57\u7b26\u3002\u6bd4\u5982\uff1a\u6c49\u5b57 \u2018\u4e2d\u2019 \u5728\u4e2d\u6587\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u4f7f\u7528 [0xD6,0xD0] \u8fd9\u4e24\u4e2a\u5b57\u8282\u5b58\u50a8\u3002 <\/p>\n\n\n\n

\u4e0d\u540c\u7684\u56fd\u5bb6\u548c\u5730\u533a\u5236\u5b9a\u4e86\u4e0d\u540c\u7684\u6807\u51c6\uff0c\u7531\u6b64\u4ea7\u751f\u4e86 GB2312\u3001GBK\u3001BIG5\u3001JIS \u7b49\u5404\u81ea\u7684\u7f16\u7801\u6807\u51c6\u3002\u8fd9\u4e9b\u4f7f\u7528 2 \u4e2a\u5b57\u8282\u6765\u4ee3\u8868\u4e00\u4e2a\u5b57\u7b26\u7684\u5404\u79cd\u6c49\u5b57\u5ef6\u4f38\u7f16\u7801\u65b9\u5f0f\uff0c\u79f0\u4e3a ANSI \u7f16\u7801\u3002\u5728\u7b80\u4f53\u4e2d\u6587\u7cfb\u7edf\u4e0b\uff0cANSI \u7f16\u7801\u4ee3\u8868 GB2312 \u7f16\u7801\uff0c\u5728\u65e5\u6587\u64cd\u4f5c\u7cfb\u7edf\u4e0b\uff0cANSI \u7f16\u7801\u4ee3\u8868 JIS \u7f16\u7801\u3002 <\/p>\n\n\n\n

\u4e0d\u540c ANSI \u7f16\u7801\u4e4b\u95f4\u4e92\u4e0d\u517c\u5bb9<\/strong>\uff0c\u5f53\u4fe1\u606f\u5728\u56fd\u9645\u95f4\u4ea4\u6d41\u65f6\uff0c\u65e0\u6cd5\u5c06\u5c5e\u4e8e\u4e24\u79cd\u8bed\u8a00\u7684\u6587\u5b57\uff0c\u5b58\u50a8\u5728\u540c\u4e00\u6bb5 ANSI \u7f16\u7801\u7684\u6587\u672c\u4e2d\u3002<\/p>\n\n\n\n

\u7b2c\u4e09\u4e2a\u9636\u6bb5<\/strong>\uff1aUNICODE\uff08\u56fd\u9645\u5316\uff09 <\/p>\n\n\n\n

\u4e3a\u4e86\u4f7f\u56fd\u9645\u95f4\u4fe1\u606f\u4ea4\u6d41\u66f4\u52a0\u65b9\u4fbf\uff0c\u56fd\u9645\u7ec4\u7ec7\u5236\u5b9a\u4e86 UNICODE \u5b57\u7b26\u96c6\uff0c\u4e3a\u5404\u79cd\u8bed\u8a00\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5b9a\u4e86\u7edf\u4e00\u5e76\u4e14\u552f\u4e00\u7684\u6570\u5b57\u7f16\u53f7\uff0c\u4ee5\u6ee1\u8db3\u8de8\u8bed\u8a00\u3001\u8de8\u5e73\u53f0\u8fdb\u884c\u6587\u672c\u8f6c\u6362\u3001\u5904\u7406\u7684\u8981\u6c42\u3002UNICODE \u5e38\u89c1\u7684\u6709\u4e09\u79cd\u7f16\u7801\u65b9\u5f0f: UTF-8 (1\u4e2a\u5b57\u8282\u8868\u793a)\u3001UTF-16 (2\u4e2a\u5b57\u8282\u8868\u793a)\u3001UTF-32 (4\u4e2a\u5b57\u8282\u8868\u793a)\u3002<\/p>\n\n\n\n

\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u6811\u72b6\u56fe\u6765\u8868\u793a\u7531ASCII\u53d1\u5c55\u800c\u6765\u7684\u5404\u4e2a\u5b57\u7b26\u96c6\u548c\u7f16\u7801\u7684\u5206\u652f\uff1a  <\/p>\n\n\n\n

\"0\"\/<\/figure>\n\n\n\n

\u5982\u679c\u8981\u66f4\u8be6\u7ec6\u5730\u4e86\u89e3\u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u7f16\u7801\u8bf7\u53c2\u8003\uff1a <\/p>\n\n\n\n

\u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u7f16\u7801\uff08Charset & Encoding\uff09<\/a><\/p>\n\n\n\n

\u53f3\u952e\u4f60\u7684\u5de5\u7a0b\u540d->Properties\uff0c\u8bbe\u7f6e\u5982\u4e0b\uff1a  <\/p>\n\n\n\n

\u56fe 2\uff1avisual studio \u7684 Character Set \u5b57\u7b26\u96c6\u8bbe\u7f6e<\/p>\n\n\n\n

\"0\"\/<\/figure>\n\n\n\n
    \n
  1. \u5f53\u8bbe\u7f6e\u4e3aUse Unicode Character Set\u65f6\uff0c\u4f1a\u6709\u9884\u7f16\u8bd1\u5b8f\uff1a_UNICODE\u3001UNICODE<\/li>\n\n\n\n
  2. \u5f53\u8bbe\u7f6e\u4e3aUse Multi-Byte Character Set\u65f6\uff0c\u4f1a\u6709\u9884\u7f16\u8bd1\u5b8f\uff1a_MBCS <\/li>\n<\/ol>\n\n\n\n

    <\/a><\/p>\n\n\n\n

    Unicode Character Set \u548c Multi-Byte Character Set\u8fd9\u4e24\u4e2a\u8bbe\u7f6e\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50: <\/strong><\/p>\n\n\n\n

    \u6709\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u7528MessageBox\u5f39\u51fa\u63d0\u793a\u6846\uff1a<\/p>\n\n\n\n

    #include “windows.h” void TestMessageBox() { ::MessageBox(NULL, “\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u7a0b\u5e8f!”, “Title”, MB_OK); }<\/p>\n\n\n\n

    \u4e0a\u9762\u8fd9\u4e2aDemo\u975e\u5e38\u7b80\u5355\u4e0d\u7528\u591a\u8bf4\u4e86\u5427\uff01\u6211\u4eec\u5c06Character Set\u8bbe\u7f6e\u4e3aMulti-Byte Character Set\u65f6\uff0c\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u548c\u8fd0\u884c\u3002\u4f46\u5f53\u6211\u4eec\u8bbe\u7f6e\u4e3aUnicode Character Set\uff0c\u5219\u4f1a\u6709\u4ee5\u4e0b\u7f16\u8bd1\u9519\u8bef\uff1a<\/p>\n\n\n\n

    error C2664: \u2018MessageBoxW\u2019 : cannot convert parameter 2 from \u2018const char [18]\u2019 to \u2018LPCWSTR\u2019<\/p>\n\n\n\n

    \u8fd9\u662f\u56e0\u4e3aMessageBox\u6709\u4e24\u4e2a\u7248\u672c\uff0c\u4e00\u4e2aMessageBoxW\u9488\u5bf9Unicode\u7248\u7684\uff0c\u4e00\u4e2a\u662fMessageBoxA\u9488\u5bf9Multi-Byte\u7684\uff0c\u5b83\u4eec\u901a\u8fc7\u4e0d\u540c\u5b8f\u8fdb\u884c\u9694\u5f00\uff0c\u9884\u8bbe\u4e0d\u540c\u7684\u5b8f\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u7248\u672c\u3002\u6211\u4eec\u4f7f\u7528\u4e86Use Unicode Character Set\u5c31\u9884\u8bbe\u4e86_UNICODE\u3001UNICODE\u5b8f\uff0c\u6240\u4ee5\u7f16\u8bd1\u65f6\u5c31\u4f1a\u4f7f\u7528MessageBoxW\uff0c\u8fd9\u65f6\u6211\u4eec\u4f20\u5165\u591a\u5b57\u8282\u5e38\u91cf\u5b57\u7b26\u4e32\u80af\u5b9a\u4f1a\u6709\u95ee\u9898\uff0c\u800c\u5e94\u8be5\u4f20\u5165\u5bbd\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u5373\u5c06\u201dTitle\u201d\u6539\u4e3aL\u201dTitle\u201d\u5c31\u53ef\u4ee5\u4e86\uff0c\u201d\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u7a0b\u5e8f!\u201d\u4e5f\u4e00\u6837\u3002<\/p>\n\n\n\n

    WINUSERAPI int WINAPI MessageBoxA( __in_opt HWND hWnd, __in_opt LPCSTR lpText, __in_opt LPCSTR lpCaption, __in UINT uType); WINUSERAPI int WINAPI MessageBoxW( __in_opt HWND hWnd, __in_opt LPCWSTR lpText, __in_opt LPCWSTR lpCaption, __in UINT uType); #ifdef UNICODE #define MessageBox MessageBoxW #else #define MessageBox MessageBoxA #endif \/\/ !UNICODE<\/p>\n\n\n\n

    \u4e0a\u9762\u7684 <\/strong>Multi-Byte Character Set <\/strong>\u4e00\u822c\u662f\u6307 <\/strong>ANSI\uff08\u591a\u5b57\u8282\uff09<\/strong>\u5b57\u7b26\u96c6<\/strong>\uff0c<\/strong>\u5173\u4e8eANSI\u8bf7\u53c2\u8003\u7b2c\u4e8c\u5c0f\u8282\u5b57\u7b26\u96c6(Charcater Set)\u4e0e\u5b57\u7b26\u7f16\u7801(Encoding) \u2014\u2014\uff08ANSI\u662f\u4e00\u79cd\u5b57\u7b26\u4ee3\u7801\uff0c\u65e9\u671f\u4e3a\u4f7f\u8ba1\u7b97\u673a\u652f\u6301\u66f4\u591a\u8bed\u8a00\uff0c\u901a\u5e38\u4f7f\u7528 0x80~0xFF \u8303\u56f4\u7684 2 \u4e2a\u5b57\u8282\u6765\u8868\u793a 1 \u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u96c6\uff0c\u5404\u4e2a\u56fd\u5bb6\u5236\u5b9a\u4e86\u5404\u81ea\u7684\u5b57\u7b26\u7f16\u7801\u89c4\u5219\uff0c\u5982ASCII\u3001ISO 8859-1\u3001BIG5\u3001GB2312\u3001GBK\u3001JIS\u7b49\u7b49\uff09\u3002<\/p>\n\n\n\n

    \u800c Unicode Character Set <\/strong>\u5c31\u662fUnicode\u5b57\u7b26\u96c6\uff0c\u4e00\u822c\u662f\u6307 <\/strong>UTF-16<\/strong> \u7f16\u7801\u7684 Unicode\u3002<\/strong>\u4e5f\u5c31\u662f\u8bf4\u6bcf\u4e2a\u5b57\u7b26\u7f16\u7801\u4e3a\u4e24\u4e2a\u5b57\u8282\uff0c\u4e24\u4e2a\u5b57\u8282\u53ef\u4ee5\u8868\u793a65535\u4e2a\u5b57\u7b26\uff0c65535\u4e2a\u5b57\u7b26\u53ef\u4ee5\u8868\u793a\u4e16\u754c\u4e0a\u5927\u90e8\u5206\u7684\u8bed\u8a00\u3002<\/p>\n\n\n\n

    \u4e00\u822c\u63a8\u8350\u4f7f\u7528Unicode\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9002\u5e94\u5404\u4e2a\u56fd\u5bb6\u8bed\u8a00\uff0c\u5728\u8fdb\u884c\u8f6f\u4ef6\u56fd\u9645\u65f6\u5c06\u4f1a\u975e\u5e38\u4fbf\u5f97\u3002\u9664\u975e\u5728\u5bf9\u5360\u7528\u5b58\u50a8\u7a7a\u95f4\u7684\u5927\u5c0f\u8981\u6c42\u975e\u5e38\u9ad8\u7684\u65f6\u5019\uff0c\u6216\u8981\u517c\u5bb9C\u7684\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u624d\u4f1a\u4f7f\u7528\u591a\u5b57\u8282\u7684\u65b9\u5f0f \u3002<\/p>\n\n\n\n

    \u7406\u89e3 _T()\u3001_Text() \u5b8f\u5373 L\u201d\u201c<\/strong><\/p>\n\n\n\n

    \u4e0a\u4e00\u5c0f\u8282<\/a>\u5bf9MessageBox\u7684\u8c03\u7528\u4e2d\u9664\u4e86\u4f7f\u7528L\u201dTitle\u201d\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528_T(\u201cTitle\u201d)\u548c_TEXT(\u201cTitle\u201d)\u3002\u800c\u4e14\u4f60\u4f1a\u53d1\u73b0\u5728MFC\u548cWin32\u7a0b\u5e8f\u4e2d\u4f1a\u66f4\u591a\u5730\u4f7f\u7528_T\u548c_TEXT\uff0c\u90a3_T\u3001_TEXT\u548cL\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n\n\n\n

    \u901a\u8fc7\u7b2c\u4e00\u5c0f\u8282\u591a\u5b57\u8282\u5b57\u7b26\u4e0e\u5bbd\u5b57\u8282\u5b57\u7b26<\/a>\u6211\u4eec\u77e5\u9053\u8868\u793a\u591a\u5b57\u8282\u5b57\u7b26(char)\u4e32\u5e38\u91cf\u65f6\u7528\u4e00\u822c\u7684\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u5c31\u53ef\u4ee5\u4e86\uff0c\u5982\u201dString test\u201d\uff1b\u800c\u8868\u793a\u5bbd\u5b57\u8282\u5b57\u7b26(wchar_t)\u4e32\u5e38\u91cf\u65f6\u8981\u5728\u5f15\u53f7\u524d\u52a0L\uff0c\u5982L\u201dString test\u201d\u3002<\/p>\n\n\n\n

    \u67e5\u770btchar.h\u5934\u6587\u4ef6\u7684\u5b9a\u4e49\u6211\u4eec\u77e5\u9053_T\u548c_TEXT\u7684\u529f\u80fd\u662f\u4e00\u6837\u7684\uff0c\u662f\u4e00\u4e2a\u9884\u5b9a\u4e49\u7684\u5b8f\u3002<\/p>\n\n\n\n

    #define _T(x) __T(x) #define _TEXT(x) __T(x)<\/p>\n\n\n\n

    \u6211\u4eec\u518d\u770b\u770b__T(x)\u7684\u5b9a\u4e49\uff0c\u53d1\u73b0\u5b83\u6709\u4e24\u4e2a\uff1a<\/p>\n\n\n\n

    #ifdef _UNICODE \/\/ … \u7701\u7565\u5176\u5b83\u4ee3\u7801 #define __T(x) L ## x \/\/ … \u7701\u7565\u5176\u5b83\u4ee3\u7801 #else \/* ndef _UNICODE *\/ \/\/ … \u7701\u7565\u5176\u5b83\u4ee3\u7801 #define __T(x) x \/\/ … \u7701\u7565\u5176\u5b83\u4ee3\u7801 #endif \/* _UNICODE *\/<\/p>\n\n\n\n

    \u8fd9\u4e0b\u660e\u767d\u4e86\u5417\uff1f\u5f53\u6211\u4eec\u7684\u5de5\u7a0b\u7684Character Set\u8bbe\u7f6e\u4e3aUse Unicode Character Set\u65f6_T\u548c_TEXT\u5c31\u4f1a\u5728\u5e38\u91cf\u5b57\u7b26\u4e32\u524d\u9762\u52a0L\uff0c\u5426\u5219(\u5373Use Multi-Byte Character Set\u65f6)\u5c31\u4f1a\u4ee5\u4e00\u822c\u7684\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/p>\n\n\n\n

    Dword\u3001LPSTR\u3001LPWSTR\u3001LPCSTR\u3001LPCWSTR\u3001LPTSTR\u3001LPCTSTR<\/strong><\/p>\n\n\n\n

    VC++\u4e2d\u8fd8\u6709\u4e00\u4e9b\u5e38\u7528\u7684\u5b8f\u4f60\u4e5f\u8bb8\u4f1a\u8303\u7cca\u6d82\uff0c\u5982Dword\u3001LPSTR\u3001LPWSTR\u3001LPCSTR\u3001LPCWSTR\u3001LPTSTR\u3001LPCTSTR\u3002\u8fd9\u91cc\u6211\u4eec\u7edf\u4e00\u603b\u7ed3\u4e00\u4e0b\uff1a <\/p>\n\n\n\n

    \u5e38\u89c1\u7684\u5b8f\uff1a<\/strong><\/p>\n\n\n\n

    \u7c7b\u578b<\/td>MBCS<\/td>UNICODE<\/td><\/tr>
    WCHAR<\/td>wchar_t<\/td>wchar_t<\/td><\/tr>
    LPSTR<\/td>char*<\/td>char*<\/td><\/tr>
    LPCSTR<\/td>const char*<\/td>const char*<\/td><\/tr>
    LPWSTR<\/td>wchar_t*<\/td>wchar_t*<\/td><\/tr>
    LPCWSTR<\/td>const wchar_t*<\/td>const wchar_t*<\/td><\/tr>
    TCHAR<\/td>char<\/td>wchar_t<\/td><\/tr>
    LPTSTR<\/td>TCHAR*(\u6216char*)<\/td>TCHAR* (\u6216wchar_t*)<\/td><\/tr>
    LPCTSTR<\/td>const TCHAR*<\/td>const TCHAR*<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n
    \n\n\n\n

    \u76f8\u4e92\u8f6c\u6362\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n

    LPWSTR->LPTSTR: W2T(); <\/p>\n\n\n\n

    LPTSTR->LPWSTR: T2W(); <\/p>\n\n\n\n

    LPCWSTR->LPCSTR: W2CT(); <\/p>\n\n\n\n

    LPCSTR->LPCWSTR: T2CW();<\/p>\n\n\n\n

    ANSI->UNICODE: A2W(); <\/p>\n\n\n\n

    UNICODE->ANSI: W2A();<\/p>\n\n\n\n

    \u5b57\u7b26\u4e32\u51fd\u6570\uff1a<\/strong><\/p>\n\n\n\n

    \u8fd8\u6709\u4e00\u4e9b\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u51fd\u6570\uff0c\u5b83\u4eec\u4e5f\u6709\u4e00 \u4e00\u5bf9\u5e94\u5173\u7cfb\uff1a<\/p>\n\n\n\n

    MBCS<\/td>UNICODE<\/td><\/tr>
    strlen();<\/td>wcslen();<\/td><\/tr>
    strcpy();<\/td>wcscpy();<\/td><\/tr>
    strcmp();<\/td>wcscmp();<\/td><\/tr>
    strcat();<\/td>wcscat();<\/td><\/tr>
    strchr();<\/td>wcschr();<\/td><\/tr>
    \u2026<\/td>\u2026<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

    \u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\u548c\u5b8f\u7684\u547d\u540d\u4f60\u4e5f\u8bb8\u5c31\u53d1\u73b0\u4e86\u4e00\u4e9b\u970d\u89c4\u5f8b\uff0c\u4e00\u822c\u5e26\u6709\u524d\u7f00w(\u6216\u540e\u7f00W)\u7684\u90fd\u662f\u7528\u4e8e\u5bbd\u5b57\u7b26\u7684\uff0c\u800c\u4e0d\u5e26\u524d\u7f00w(\u6216\u5e26\u6709\u540e\u7f00A)\u7684\u4e00\u822c\u662f\u7528\u4e8e\u591a\u5b57\u8282\u5b57\u7b26\u7684\u3002<\/p>\n\n\n\n

    <\/a>\u7406\u89e3CString\u4ea7\u751f\u7684\u539f\u56e0\u4e0e\u5de5\u4f5c\u7684\u673a\u7406<\/strong><\/p>\n\n\n\n

    CString\uff1a\u52a8\u6001\u7684TCHAR\u6570\u7ec4\uff0c\u662f\u5bf9TCHAR\u6570\u7ec4\u7684\u4e00\u79cd\u5c01\u95ed\u3002\u5b83\u662f\u4e00\u4e2a\u5b8c\u5168\u72ec\u7acb\u7684\u7c7b\uff0c\u5c01\u88c5\u4e86\u201c+\u201d\u7b49\u64cd\u4f5c\u7b26\u548c\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u6362\u53e5\u8bdd\u8bf4\u5c31\u662fCString\u662f\u5bf9TCHAR\u64cd\u4f5c\u7684\u65b9\u6cd5\u7684\u96c6\u5408\u3002\u5b83\u7684\u4f5c\u7528\u662f\u65b9\u4fbfWIN32\u7a0b\u5e8f\u548cMFC\u7a0b\u5e8f\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u5904\u7406\u548c\u7c7b\u578b\u7684\u8f6c\u6362\u3002<\/p>\n\n\n\n

    \u5173\u4e8eCString\u66f4\u8be6\u7ec6\u7684\u7528\u6cd5\u8bf7\u53c2\u8003\uff1a <\/p>\n\n\n\n

    CString\u4e0estring\u3001char*\u7684\u533a\u522b\u548c\u8f6c\u6362<\/a><\/p>\n\n\n\n

    CString\u7684\u5e38\u89c1\u7528\u6cd5<\/a><\/p>\n\n\n\n

    \u53c2\u8003\u6587\u7ae0\uff1a <\/p>\n\n\n\n

    \u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u7f16\u7801\uff08Charset & Encoding\uff09<\/a><\/p>\n\n\n\n

    \u5b57\u7b26\uff0c\u5b57\u8282\u548c\u7f16\u7801<\/a><\/p>\n\n\n\n

    \u300awindows\u6838\u5fc3\u7f16\u7a0b\u7cfb\u5217\u300b\u4e8c\u8c08\u8c08ANSI\u548cUnicode\u5b57\u7b26\u96c6<\/a><\/p>\n\n\n\n

    Dword\u3001LPSTR\u3001LPWSTR\u3001LPCSTR\u3001LPCWSTR\u3001LPTSTR\u3001LPCTSTR<\/a><\/p>\n\n\n\n

    \u8f6c\u81ea\uff1ahttp:\/\/blog.csdn.net\/luoweifu\/article\/details\/49382969<\/a><\/p>\n\n\n\n

    https:\/\/www.lmonkey.com\/t\/DExgl2ZBK<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

    \u8f6c\uff1a \u5e26\u4f60\u73a9\u8f6cVisual Studio\u2014\u2014\u5e26\u4f60\u7406\u89e3\u591a\u5b57\u8282\u7f16\u7801\u4e0eUn...<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/22"}],"collection":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":1,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":23,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/22\/revisions\/23"}],"wp:attachment":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}