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);
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM