如何将utf16 ushort数组转换为utf8 std :: string? [英] How to convert a utf16 ushort array to a utf8 std::string?

查看:647
本文介绍了如何将utf16 ushort数组转换为utf8 std :: string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在写一个插件,它只是一个现有库的包装。
插件的主机传递给我一个utf-16格式的字符串,定义如下



typedef unsigned short PA_Unichar;



并且包裹的库只接受一个const char *或一个std :: string utf-8格式化的字符串
我试着编写一个转换函数

  std :: string toUtf8(const PA_Unichar * data)
{
std :: wstring_convert< std :: codecvt_utf8_utf16< char16_t>,char16_t>兑换;
return std :: string(convert.to_bytes(static_cast< const char16_t *>(data));
}

但是显然这不起作用,引发了一个编译错误static_cast从'const指针'(也称为'const unsigned short *')''不允许'const char16_t *' / p>

那么,最优雅/正确的方法是什么?




解决方案

您可以将 PA_unichar 字符串转换为使用 basic_string(Iterator,Iterator)构造函数,然后使用 std :: codecvt_utf8_utf16 facet:

  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);
}

我认为测试这个,因为我的实现不支持它。我有一个实现 wstring_convert ,我计划包括在GCC 4.9,但我没有 codecvt_utf8_utf16 以测试它。


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

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

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"

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:

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.

这篇关于如何将utf16 ushort数组转换为utf8 std :: string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆