How to convert a utf16 ushort array to a utf8 std::string?

馋奶兔 提交于 2019-12-06 01:59:43

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/13890953/how-to-convert-a-utf16-ushort-array-to-a-utf8-stdstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!