[英]How to convert a utf16 ushort array to a utf8 std::string?
Currently I'm writing a plugin which is just a wrapper around an existing library. 目前,我正在编写一个插件,该插件只是现有库的包装。 The plugin's host passes to me an utf-16 formatted string defined as following
插件的主机向我传递了一个utf-16格式的字符串,定义如下
typedef unsigned short PA_Unichar; typedef unsigned short PA_Unichar;
And the wrapped library accepts only a const char* or a std::string utf-8 formatted string I tried writing a conversion function like 包装好的库仅接受const char *或std :: string utf-8格式的字符串,我尝试编写转换函数,例如
std::string toUtf8(const PA_Unichar* data)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
return std::string(convert.to_bytes(static_cast<const char16_t*>(data));
}
But obviously this doesn't work, throwing me a compile error "static_cast from 'const pointer' (aka 'const unsigned short*') to 'const char16_t *' is not allowed" 但这显然是行不通的,并抛出了一个编译错误“不允许从“常量指针”(也称为“常量无符号短符号*”)到“常量char16_t *”的static_cast”
So what's the most elegant/correct way to do it? 那么,最优雅/最正确的方法是什么?
Thank you in advance. 先感谢您。
You could convert the PA_unichar
string to a string of char16_t
using the basic_string(Iterator, Iterator)
constructor, then use the std::codecvt_utf8_utf16
facet as you attempted: 您可以使用
basic_string(Iterator, Iterator)
构造函数将PA_unichar
字符串转换为char16_t
字符串,然后在尝试时使用std::codecvt_utf8_utf16
构面:
std::string conv(const PA_unichar* str, size_t len)
{
std::u16string s(str, str+len);
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
return convert.to_bytes(s);
}
I think that's right. 我认为是对的。 Unfortunately I can't test this, as my implementation doesn't support it yet.
不幸的是,我无法对其进行测试,因为我的实现尚不支持它。 I have an implementation of
wstring_convert
which I plan to include in GCC 4.9, but I don't have an implementation of codecvt_utf8_utf16
to test it with. 我有
wstring_convert
的实现,我打算将其包含在GCC 4.9中,但是我没有codecvt_utf8_utf16
的实现来进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.