字符集互转的方法

#include <string>
#include <boost/locale.hpp>

#include <boost/program_options/detail/convert.hpp>
#include <boost/program_options/detail/utf8_codecvt_facet.hpp>

#include <codecvt>
#include <string>

// convert UTF-8 string to wstring
std::wstring utf8_to_wstring(const std::string& str)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
    return myconv.from_bytes(str);
}

// convert wstring to UTF-8 string
std::string wstring_to_utf8(const std::wstring& str)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
    return myconv.to_bytes(str);
}

void f() {
    int x=0;
    x += 1;
}

int	main(void)
{
    char utf8_str[] = { 0xE6 ,0xB5 ,0x8B ,0xE8 ,0xAF ,0x95, 0x00 };  //"测试"的utf8编码
    char gbk_str[] =  { 0xB2 ,0xE2 ,0xCA ,0xD4 ,0x00 }; //"测试"的GBK编码
    wchar_t ucs16_str[] ={ 0x6D4B ,0x8BD5 ,0x0000 }; //"测试"的unicode 16 BE 编码
    
    std::string ret1;
    std::wstring ret2;

    //GBK字符串 转 utf8字符串
    ret1 = boost::locale::conv::to_utf<char>(gbk_str, "GBK");

    //GBK字符串 转 unicode字符串
    ret2 = boost::locale::conv::to_utf<wchar_t>(gbk_str, "GBK");
    
    //utf8字符串 转 GBK(由string保存的)
    ret1 = boost::locale::conv::from_utf<char>(utf8_str, "GBK");

    //utf8字符串 转 unicode(由wstring保存的)
    ret2 = boost::locale::conv::utf_to_utf<wchar_t>(utf8_str);

    //错误的代码
    //ret2 = boost::locale::conv::utf_to_utf<wchar_t>(utf8_str,"GBK"); X
    //直接导致CPU 100%。因为它调用了utf_to_utf(const char* begin, const char* end)
    //utf8_str和"GBK"不是匹配的迭代器

    //unicode字符串 转 utf8
    ret1 = boost::locale::conv::utf_to_utf<char>(ucs16_str, ucs16_str+2);

    //unicode字符串 转 GBK字符串。注意,from...函数,模板参数同函数的文本参数
    //                                  to...函数,模板参数同返回值
    ret1 = boost::locale::conv::from_utf<wchar_t>(ucs16_str, ucs16_str+2, "GBK");

    //C++11 提供的utf8 转 ucs16
    ret2 = utf8_to_wstring(utf8_str);

    //C++11 提供的ucs16 转 utf8
    ret1 = wstring_to_utf8(ucs16_str);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值