奋力转换矢量&lt;&字符GT;到wstring的 [英] Struggling to convert vector<char> to wstring
问题描述
我需要UTF16文本转换为UTF8。实际转换code是简单的:
I need to convert utf16 text to utf8. The actual conversion code is simple:
std::wstring in(...);
std::string out = boost::locale::conv::utf_to_utf<char, wchar_t>(in);
然而问题是,UTF16被从文件中读取并且它可以或可以不包含的BOM。我的code需要便携式(最小的是windows / OSX / Linux)的。我真的在努力弄清楚如何从字节序列创建一个 wstring的
。
编辑:这是不是链接的问题的副本,在这个问题的任择议定书需要一个宽字符串转换成字节数组 - 我需要周围的其他方法进行转换。
this is not a duplicate of the linked question, as in that question the OP needs to convert a wide string into an array of bytes - and I need to convert the other way around.
推荐答案
您不应该在所有你的情况使用广泛的类型。
You should not use wide types at all in your case.
假设你可以得到一个的char *
从矢量&lt;字符&GT;
,你能坚持字节使用以下code:
Assuming you can get a char *
from your vector<char>
, you can stick to bytes by using the following code:
char * utf16_buffer = &my_vector_of_chars[0];
char * buffer_end = &my_vector_of_chars[vector.size()];
std::string utf8_str = boost::locale::conv::between(utf16_buffer, buffer_end, "UTF-8", "UTF-16");
<一个href=\"http://www.boost.org/doc/libs/1_55_0/libs/locale/doc/html/group__$c$cpage.html#ga7eda9058b8cbac9b05886bcc894faeff\"相对=nofollow>运行在8位字符之间,并可以让你避免转换为16位字符完全。
between operates on 8-bit characters and allows you to avoid conversion to 16-bit characters altogether.
有必要使用,使用指针到缓冲区的结束,因为默认情况下,
之间超载>将停在第一个'\\ 0'
字符字符串中,这将是几乎立即,因为输入是UTF-16。
It is necessary to use the between
overload that uses the pointer to the buffer's end, because by default, between
will stop at the first '\0'
character in the string, which will be almost immediately because the input is UTF-16.
这篇关于奋力转换矢量&lt;&字符GT;到wstring的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!