【发布时间】:2012-10-14 10:00:36
【问题描述】:
我有一个 std::string 输出。使用 utf8proc 我想将其转换为有效的 utf8 字符串。 http://www.public-software-group.org/utf8proc-documentation
typedef int int32_t;
#define ssize_t int
ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options)
Reencodes the sequence of unicode characters given by the pointer buffer and length as UTF-8. The result is stored in the same memory area where the data is read. Following flags in the options field are regarded: (Documentation missing here) In case of success the length of the resulting UTF-8 string is returned, otherwise a negative error code is returned.
WARNING: The amount of free space being pointed to by buffer, has to exceed the amount of the input data by one byte, and the entries of the array pointed to by str have to be in the range of 0x0000 to 0x10FFFF, otherwise the program might crash!
首先,如何在末尾添加一个额外的字节?那么如何从 std::string 转换为 int32_t *buffer?
这不起作用:
std::string g = output();
fprintf(stdout,"str: %s\n",g.c_str());
g += " "; //add an extra byte??
g = utf8proc_reencode((int*)g.c_str(), g.size()-1, 0);
fprintf(stdout,"strutf8: %s\n",g.c_str());
【问题讨论】:
-
std::string只是一个字节序列。你的源std::string是什么编码? -
每次在 C++ 程序中看到
printf时,我都会感到畏缩,尤其是输出字符串。 -
@Charles Bailey:输出并不总是相同的编码。通常它是 utf8,但有时它是我现在知道的一些编码。
-
“有时它是我现在知道的一些编码” - 你有一个根本问题,你不能通过盲目地向它扔代码来解决。您需要知道您的字符串的编码是什么,然后才能明智地将其转换为任何其他编码。
-
我不知道,因为我从 iptc 数据中读取...