std::u32string conversion to/from std::string and std::u16string
I need to convert between UTF-8, UTF-16 and UTF-32 for different API's/modules and since I know have the option to use C++11 am looking at the new string types.
It looks like I can use string
, u16string
and u32string
for UTF-8, UTF-16 and UTF-32. I also found codecvt_utf8
and codecvt_utf16
which look to be able to do a conversion between char
or char16_t
and char32_t
and what looks like a higher level wstring_convert
but that only appears to work with bytes/std::string
and not a great deal of documentation.
Am I meant to use a wstring_convert
somehow for the UTF-16 ↔ UTF-32 and UTF-8 ↔ UTF-32 case? I only really found examples for UTF-8 to UTF-16, which I am not even sure will be correct on Linux where wchar_t
is normally considered UTF-32... Or do something more complex with those codecvt things directly?
Or is this just still not really in a usable state and I should stick with my own existing small routines using 8, 16 and 32bit unsigned integers?
---------------------------------------------------------------------------------------------------------------------------------------------------------------
answer:
If you read the documentation at CppReference.com for wstring_convert
, codecvt_utf8
, codecvt_utf16
, and codecvt_utf8_utf16
, the pages include a table that tells you exactly what you can use for the various UTF conversions.
And yes, you would use std::wstring_convert
to facilitate the conversion between the various UTFs. Despite its name, it is not limited to just std::wstring
, it actually operates with any std::basic_string
type (which std::string
, std::wstring
, and std::uXXstring
are all based on).
Class template std::wstring_convert performs conversions between byte string
std::string
and wide stringstd::basic_string<Elem>
, using an individual code conversion facet Codecvt. std::wstring_convert assumes ownership of the conversion facet, and cannot use a facet managed by a locale. The standard facets suitable for use with std::wstring_convert are std::codecvt_utf8 for UTF-8/UCS2 and UTF-8/UCS4 conversions and std::codecvt_utf8_utf16 for UTF-8/UTF-16 conversions.
For example:
typedef std::string u8string;
u8string To_UTF8(const std::u16string &s)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return conv.to_bytes(s);
}
u8string To_UTF8(const std::u32string &s)
{
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
return conv.to_bytes(s);
}
std::u16string To_UTF16(const u8string &s)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
return conv.from_bytes(s);
}
std::u16string To_UTF16(const std::u32string &s)
{
std::wstring_convert<std::codecvt_utf16<char32_t>, char32_t> conv;
std::string bytes = conv.to_bytes(s);
return std::u16string(reinterpret_cast<const char16_t*>(bytes.c_str()), bytes.length()/sizeof(char16_t));
}
std::u32string To_UTF32(const u8string &s)
{
std::wstring_convert<codecvt_utf8<char32_t>, char32_t> conv;
return conv.from_bytes(s);
}
std::u32string To_UTF32(const std::u16string &s)
{
const char16_t *pData = s.c_str();
std::wstring_convert<std::codecvt_utf16<char32_t>, char32_t> conv;
return conv.from_bytes(reinterpret_cast<const char*>(pData), reinterpret_cast<const char*>(pData+s.length()));
}
std::u32string conversion to/from std::string and std::u16string的更多相关文章
- 对std::string和std::wstring区别的解释,807个赞同,有例子
807down vote string? wstring? std::string is a basic_string templated on a char, and std::wstring on ...
- C++ MFC std::string转为 std::wstring
std::string转为 std::wstring std::wstring UTF8_To_UTF16(const std::string& source) { unsigned long ...
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
- no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string
问题: t->package().ship_id(sqlRow[1]);其中 ship_id为 结构体package中的string类型.如下: typedef struct Package{ ...
- Item 25: 对右值引用使用std::move,对universal引用则使用std::forward
本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 右值引用只能绑定那些有资格被move的对象上去.如 ...
- 如何使用 window api 转换字符集?(std::string与std::wstring的相互转换)
//宽字符转多字节 std::string W2A(const std::wstring& utf8) { int buffSize = WideCharToMultiByte(CP_ACP, ...
- std::string与std::wstring互相转换
作者:zzandyc来源:CSDN原文:https ://blog.csdn.net/zzandyc/article/details/77540056 版权声明:本文为博主原创文章,转载请附上博文链接 ...
- 实战c++中的string系列--std::string与MFC中CString的转换
搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...
- gcc编译链接std::__cxx11::string和std::string的问题
今天公司的小伙伴遇到一个问题,这里做一个记录. 问题是这样的,他编译了公司的基础库,然后在程序中链接的时候遇到点问题,报错找不到定义. 用到的函数声明大概是这样的: void function(con ...
随机推荐
- Linux下yum命令详解
yum是什么yum = Yellow dog Updater, Modified主要功能是更方便的添加/删除/更新RPM包.它能自动解决包的倚赖性问题.它能便于管理大量系统的更新问题yum特点 可以同 ...
- JAVA的get post 区别
1. get 是从服务器上获取数据,post 是向服务器传送数据. get 请求返回 request - URI 所指出的任意信息.Post 请求用来发送电子邮件.新闻或发送能由交互用户填写的表格.这 ...
- lakala GradientBoostedTrees
/** * Created by lkl on 2017/12/6. */ import org.apache.spark.mllib.evaluation.BinaryClassificationM ...
- 在PHP中使用curl_init函数的说明
$ch = curl_init(); $c_url = 'http://www.baidu.com'; $c_url_data = "product_&type=".$ty ...
- __setup、early_param的解析
内核初始化时根据字符串匹配获得相应的处理函数,查找的时候有些麻烦. 写个脚本对将内核中的__setup和early_param显式做了解析: __setup #! /bin/bash grep '\& ...
- Python学习之——编码方式
1.各种编码方式 ASCII:http://zh.wikipedia.org/zh-hans/ASCII Unicode:http://zh.wikipedia.org/zh-hans/Unicode ...
- 分页功能实现之通过ajax实现表单内容刷新
拿代码来说话 我们的需求就是点击翻页功能,实现表格内容局部刷新且能够翻到对应的页面上,不明白? 那么就看看下面的图,需要达到的效果如下所示: 现在要实现的功能就是把红线框起来的表单内容 在点击翻页的时 ...
- bat 变量作用域
set answer=one if true equ true ( set answer=two echo %answer% ) echo Argument is %answer% pause
- javascript关于onclick()
1 <html> <head> <title>js1 </title> <style> #content{ margin:0 auto; t ...
- mysql中显示当前数据库下的所有表,包括视图。
环境说明: mysql版本:5.5.57-log 操作系统:Red Hat Enterprise Linux Server release 6.6 (Santiago) 需求:查看当前数据库下所有的表 ...