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