boost-字符编码转换:使用conv

  Windows下的字符集转换可以使用WideCharToMultiByte/ MultiByteToWideChar,Linux下字符集转换可以使用iconv()函数,下面为使用boost的conv来进行字符集转换:

#include <boost/locale/encoding.hpp>

string UTF8toGBK(const string & str)
{
    return boost::locale::conv::between(str, "GBK", "UTF-8");
}

string GBKtoUTF8(const string & str)
{
    return boost::locale::conv::between(str, "UTF-8", "GBK");
}

wstring GBKtoUNICODE(const string & str)
{
    return boost::locale::conv::to_utf<wchar_t>(str, "GBK");
}

string UNICODEtoGBK(wstring wstr)
{
    return boost::locale::conv::from_utf(wstr, "GBK");
}

string UNICODEtoUTF8(const wstring& wstr)
{
    return boost::locale::conv::from_utf(wstr, "UTF-8");
}

wstring UTF8toUNICODE(const string & str)
{
    return boost::locale::conv::utf_to_utf<wchar_t>(str);
}
原文地址:https://www.cnblogs.com/milanleon/p/7442920.html