Thread: How to set up libconv in visual c++

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    13

    How to set up libconv in visual c++

    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?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This blog post, found with this Google search might help.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you're not trying to write something portable, then you can use the following Windows specific code:
    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
    Windows codepage 936 is for GBK, which is an extension of GB2312.
    Code:
        string gbk;
        ...
        wstring utf16 = str_to_wstr(gbk, 936);
        string utf8 = wstr_to_str(utf16, CP_UTF8);
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCL Scripting
    By johndejesus in forum Tech Board
    Replies: 5
    Last Post: 08-07-2009, 03:35 AM
  2. SEt Visual Studio 2005 set .dll locations?
    By mhandlon in forum C++ Programming
    Replies: 6
    Last Post: 01-19-2006, 03:00 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 12:23 PM

Tags for this Thread