C++常用字符集转换方法一

3 篇文章 0 订阅
2 篇文章 0 订阅

C++常用字符转换UTF8到string类型:
直接使用boost库中函数,在编译的时候要链接libboost_locale.so 库;此方法可以直接跨平台使用,在linux、window下都适用
例如:在代码中这接写中文转UTF8码,可以使用如下接口
string str=“中国”
string utf8Str = GB2312toUTF8(str);

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

std::string UTF8toGBK(const std::string& str);

std::string GBKtoUTF8(const std::string& str);

std::string UTF8toGB2312(const std::string& str);

std::string GB2312toUTF8(const std::string& str);

std::wstring GBKtoUNICODE(const std::string& str);

std::string UNICODEtoGBK(std::wstring wstr);

std::string UNICODEtoUTF8(const std::wstring& wstr);

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

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

std::string UTF8toGB2312(const std::string& str)
{
	return boost::locale::conv::between(str, "GBK", "GB2312");
}

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

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

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

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

std::wstring UTF8toUNICODE(const std::string& str)
{
	return boost::locale::conv::utf_to_utf<wchar_t>(str);
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值