boost::locale::conv:: 字符编码转换

相关字符集转换函数功能摘要:

std::string boost::locale::conv::between	(	char const * 	begin,
 	 	char const * 	end,
 	 	std::string const & 	to_encoding,
 	 	std::string const & 	from_encoding,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text in range [begin,end) to to_encoding from from_encoding
std::string boost::locale::conv::between	(	char const * 	text,
 	 	std::string const & 	to_encoding,
 	 	std::string const & 	from_encoding,
 	 	method_type 	how = default_method 
 	)	 	[inline]
Convert a text to to_encoding from from_encoding
std::string boost::locale::conv::between	(	std::string const & 	text,
 	 	std::string const & 	to_encoding,
 	 	std::string const & 	from_encoding,
 	 	method_type 	how = default_method 
 	)	 	[inline]
Convert a text to to_encoding from from_encoding
template<typename CharType >
std::string boost::locale::conv::from_utf	(	CharType const * 	begin,
 	 	CharType const * 	end,
 	 	std::string const & 	charset,
 	 	method_type 	how = default_method 
 	)	 	 
convert UTF text in range [begin,end) to a text encoded with charset according to policy how
template<typename CharType >
std::string boost::locale::conv::from_utf	(	CharType const * 	begin,
 	 	CharType const * 	end,
 	 	std::locale const & 	loc,
 	 	method_type 	how = default_method 
 	)	 	 
convert UTF text in range [begin,end) to a text encoded according to locale loc according to policy how
Note:
throws std::bad_cast if the loc does not have info facet installed
template<typename CharType >
std::string boost::locale::conv::from_utf	(	std::basic_string< CharType > const & 	text,
 	 	std::string const & 	charset,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text from charset to UTF string
template<typename CharType >
std::string boost::locale::conv::from_utf	(	CharType const * 	text,
 	 	std::string const & 	charset,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text from UTF to charset
template<typename CharType >
std::string boost::locale::conv::from_utf	(	std::basic_string< CharType > const & 	text,
 	 	std::locale const & 	loc,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text in UTF to locale encoding given by loc
Note:
throws std::bad_cast if the loc does not have info facet installed
template<typename CharType >
std::string boost::locale::conv::from_utf	(	CharType const * 	text,
 	 	std::locale const & 	loc,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text in UTF to locale encoding given by loc
Note:
throws std::bad_cast if the loc does not have info facet installed
template<typename CharType >
std::basic_string<CharType> boost::locale::conv::to_utf	(	char const * 	begin,
 	 	char const * 	end,
 	 	std::string const & 	charset,
 	 	method_type 	how = default_method 
 	)	 	 
convert string to UTF string from text in range [begin,end) encoded with charset according to policy how
template<typename CharType >
std::basic_string<CharType> boost::locale::conv::to_utf	(	char const * 	begin,
 	 	char const * 	end,
 	 	std::locale const & 	loc,
 	 	method_type 	how = default_method 
 	)	 	 
convert string to UTF string from text in range [begin,end) encoded according to locale loc according to policy how
Note:
throws std::bad_cast if the loc does not have info facet installed
template<typename CharType >
std::basic_string<CharType> boost::locale::conv::to_utf	(	std::string const & 	text,
 	 	std::string const & 	charset,
 	 	method_type 	how = default_method 
 	)	 	 
convert a string text encoded with charset to UTF string
template<typename CharType >
std::basic_string<CharType> boost::locale::conv::to_utf	(	char const * 	text,
 	 	std::string const & 	charset,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text from charset to UTF string
template<typename CharType >
std::basic_string<CharType> boost::locale::conv::to_utf	(	std::string const & 	text,
 	 	std::locale const & 	loc,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text in locale encoding given by loc to UTF
Note:
throws std::bad_cast if the loc does not have info facet installed
template<typename CharType >
std::basic_string<CharType> boost::locale::conv::to_utf	(	char const * 	text,
 	 	std::locale const & 	loc,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a text in locale encoding given by loc to UTF
Note:
throws std::bad_cast if the loc does not have info facet installed
template<typename CharOut , typename CharIn >
std::basic_string<CharOut> boost::locale::conv::utf_to_utf	(	CharIn const * 	begin,
 	 	CharIn const * 	end,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a Unicode text in range [begin,end) to other Unicode encoding
template<typename CharOut , typename CharIn >
std::basic_string<CharOut> boost::locale::conv::utf_to_utf	(	CharIn const * 	str,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a Unicode NUL terminated string str other Unicode encoding
template<typename CharOut , typename CharIn >
std::basic_string<CharOut> boost::locale::conv::utf_to_utf	(	std::basic_string< CharIn > const & 	str,
 	 	method_type 	how = default_method 
 	)	 	 
Convert a Unicode string str other Unicode encoding


简单示例:
1、between把 source 这个字串从BIG5 转换到 UTF-8:

string source = "BIG5字符串";  
string s = boost::locale::conv::between( source, "UTF-8", "BIG5" ); 


2、to_utf可以输出 string ,也可以输出成 wstring。像下面的例子,就是把 sSource 这个 BIG-5 编码的字串,依序转换成 wstring 和 string 的字串。

string sSource = "BIG-5字符串";  
wstring ws = boost::locale::conv::to_utf<wchar_t>( sSource, "BIG5" );  
string  ss = boost::locale::conv::to_utf<char>( sSource, "BIG5" );  

3、from_utf 是把 UTF 字串(string 或wstring)、转换为特定编码的字串用的,他可以转换string 或wstring 的字串,但是输出一定是string。
下面的例子,就是把 sSource 和 wSource 这两个 UTF 字串,都转换成 BIG-5 的 string 字串。


string  sSource =  "字符串";  
wstring wSource = L"...";  
string  ss1 = boost::locale::conv::from_utf( wSource, "BIG5" );  
string  ss2 = boost::locale::conv::from_utf( sSource, "BIG5" );  


4、utf_to_utf的目的,是在 UTF 的 string 字串和 wstring 字串之间做转换,下面的例子,就是把类型是 string 的 sSource 转换成 wstring、并把类型是 wstring 的 wSource 转换成 string

string  sSource =  "...";  
wstring wSource = L"...";  
wstring wStr = boost::locale::conv::utf_to_utf<wchar_t>( sSource );  
string  sStr = boost::locale::conv::utf_to_utf<char>( wSource );  








  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值