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