I need to convert the gb2312 encoding to utf_8 , and i have learned that libconv can help me to do that , but my problem is how to set up the library in visual c++ . IS there any tutorial?
This is a discussion on How to set up libconv in visual c++ within the C++ Programming forums, part of the General Programming Boards category; I need to convert the gb2312 encoding to utf_8 , and i have learned that libconv can help me to ...
I need to convert the gb2312 encoding to utf_8 , and i have learned that libconv can help me to do that , but my problem is how to set up the library in visual c++ . IS there any tutorial?
This blog post, found with this Google search might help.
If you're not trying to write something portable, then you can use the following Windows specific code:
Windows codepage 936 is for GBK, which is an extension of GB2312.Code:#include <windows.h> #include <string> #include <vector> std::wstring str_to_wstr(const std::string &str, UINT cp = CP_ACP) { int len = MultiByteToWideChar(cp, 0, str.c_str(), str.length(), 0, 0); if (!len) return L"ErrorA2W"; std::vector<wchar_t> wbuff(len + 1); // NOTE: this does not NULL terminate the string in wbuff, but this is ok // since it was zero-initialized in the vector constructor if (!MultiByteToWideChar(cp, 0, str.c_str(), str.length(), &wbuff[0], len)) return L"ErrorA2W"; return &wbuff[0]; }//str_to_wstr std::string wstr_to_str(const std::wstring &wstr, UINT cp = CP_ACP) { int len = WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(), 0, 0, 0, 0); if (!len) return "ErrorW2A"; std::vector<char> abuff(len + 1); // NOTE: this does not NULL terminate the string in abuff, but this is ok // since it was zero-initialized in the vector constructor if (!WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(), &abuff[0], len, 0, 0)) { return "ErrorW2A"; }//if return &abuff[0]; }//wstr_to_str
ggCode:string gbk; ... wstring utf16 = str_to_wstr(gbk, 936); string utf8 = wstr_to_str(utf16, CP_UTF8);
Last edited by Codeplug; 09-18-2009 at 12:47 PM.