GBK到UTF8编码转换的C++实现
2013-12-02 22:20
507 查看
std::string GBKToUTF8(const std::string& strGBK) { WCHAR * str1; int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0); str1 = new WCHAR ; MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n); n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL); char * str2 = new char ; WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); std::string strOutUTF8; strOutUTF8.assign(str2,n); delete[]str1; str1 = NULL; delete[]str2; str2 = NULL; return strOutUTF8; } std::string UTF8ToGBK(const std::string& strUTF8) { int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); WCHAR* wszGBK = new WCHAR[len + 1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK = new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL); //strUTF8 = szGBK; std::string strTemp; strTemp.assign(szGBK,len); delete[]szGBK; delete[]wszGBK; return strTemp; }
相关文章推荐
- Keil C51对C语言的关键词扩展之十八:using
- 哈希表的C语言实现
- Sample 4.21:newstrct.cpp
- C++中 int i 与 int &i 注意事项
- Sample 4.20:ptrstr.cpp
- Sample 4.19:addpntrs.cpp
- Sample 4.18:arraynew.cpp
- Sample 4.17:use_new.cpp
- Sample 4.16:init_ptr.cpp
- Sample 4.15:pointer.cpp
- Sample 4.14:address.cpp
- Sample 4.13:arrstruct.cpp
- C语言动态存储分配
- Sample 4.12:assgn_st.cpp
- Sample 4.11:structur.cpp
- Sample 4.9:strtype3.cpp
- Sample 4.8:strtype2.cpp
- Sample 4.7:strtype1.cpp
- Sample 4.6:numstr.cpp
- Sample 4.5:instr3.cpp