目錄
第1章說明
VC++中寬窄字符串的相互轉換比較麻煩,借助std::string能大大減少代碼量。
1.1 代碼
函數聲明如下:
std::string stringA2W(const char* pA,int nA,UINT uCodePage = CP_ACP); std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage = CP_ACP); std::string stringA2W(const std::string&sA,UINT uCodePage = CP_ACP); std::string stringW2A(const std::string&sW,UINT uCodePage = CP_ACP); |
函數實現如下:
/***************************************************************\ 窄字符串 ==> 寬字符串(UTF16LE) pA [in] 窄字符串首地址 nA [in] 窄字符串長度。小於零就使用 strlen 計算長度。 uCodePage [in] 窄字符串的代碼頁 如:CP_ACP 表示系統默認;936 表示 GBK…… 返回: 寬字符串 sW 寬字符串的字符數 nChar = sW.length() / sizeof(wchar_t) 寬字符串的字節數 nByte = (nChar + 1) * sizeof(wchar_t) - 1 字節數多加了 sizeof(wchar_t) - 1 = 1 個 \0,是為了下面的用法 const wchar_t* pW = (const wchar_t*)sW.c_str(); \***************************************************************/ std::string stringA2W(const char*pA,int nA,UINT uCodePage) { std::string sW; if(pA) { if(nA < 0) { nA = strlen(pA); } if(nA > 0) { int nW = MultiByteToWideChar(uCodePage,0,pA,nA,NULL,0); if(nW > 0) { int nByte = (nW + 1) * sizeof(wchar_t); wchar_t*pW = (wchar_t*)malloc(nByte); if(pW) { MultiByteToWideChar(uCodePage,0,pA,nA,pW,nW); pW[nW] = L'\0'; sW.assign((const char*)pW,nByte - 1); free(pW); } } } } if(sW.empty()) { sW = std::string(sizeof(wchar_t) - 1,'\0'); } return sW; } /***************************************************************\ 窄字符串 ==> 寬字符串(UTF16LE) sA [in] 窄字符串 uCodePage [in] 窄字符串的代碼頁 如:CP_ACP 表示系統默認;936 表示 GBK…… 返回:寬字符串 \***************************************************************/ std::string stringA2W(const std::string&sA,UINT uCodePage) { return stringA2W(sA.c_str(),sA.length(),uCodePage); } /***************************************************************\ 寬字符串(UTF16LE) ==> 窄字符串 pW [in] 寬字符串首地址 nW [in] 寬字符串字符數。小於零就使用 wcslen 計算長度。 uCodePage [in] 窄字符串的代碼頁 如:CP_ACP 表示系統默認;936 表示 GBK…… 返回:窄字符串 \***************************************************************/ std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage) { std::string sA; if(pW) { if(nW < 0) { nW = wcslen(pW); } if(nW > 0) { int nA = WideCharToMultiByte(uCodePage,0,pW,nW ,NULL,NULL,NULL,NULL); if(nA > 0) { char*pA = (char*)malloc(nA); if(pA) { WideCharToMultiByte(uCodePage,0,pW,nW ,pA,nA,NULL,NULL); sA.assign(pA,nA); free(pA); } } } } return sA; } /***************************************************************\ 寬字符串(UTF16LE) ==> 窄字符串 sW [in] 寬字符串,編碼為 UTF16LE uCodePage [in] 窄字符串的代碼頁 如:CP_ACP 表示系統默認;936 表示 GBK…… 返回:窄字符串 \***************************************************************/ std::string stringW2A(const std::string&sW,UINT uCodePage) { return stringW2A((const wchar_t*)sW.c_str() ,sW.length() / sizeof(wchar_t),uCodePage); } |
1.2 使用
有了上述四個函數,字符串的編碼轉換用一、兩行代碼即可實現。
如:將GBK字符串"測試"轉換為寬字符串
std::string sW = stringA2W("測試"); |
//簡體中文 Windows 下 |
std::string sW = stringA2W("測試",936); |
//安裝有代碼頁936的Windows |
如:將GBK字符串"測試"轉換為UTF-8編碼
std::string sUTF8 = stringW2A(stringA2W("測試",936),CP_UTF8); |
如:將GBK字符串"測試"轉換為Big5編碼
std::string sBig5 = stringW2A(stringA2W("測試",936),950); |