如何在不添加字节顺序标记的情况下使iconv转换为UTF- *编码?
这个代码显示了这个问题,在64位Linux下用G ++ 4.8编译:
#include <iostream>
#include <iconv.h>
#include <cstdint>
#include <iomanip>
using namespace std;
int main() {
const wchar_t *ws = L"Hello, world.\n";
for(size_t ii = 0; ii < 15; ii++) {
cout << setw(2) << hex << uint32_t(ws[ii]) << " ";
}
cout << endl;
iconv_t conv = iconv_open("UTF-16", "UTF-32");
char *outbuf = new char[14 * 4];
char *inptr = const_cast<char*>(reinterpret_cast<const char *>(ws));
char *outptr = outbuf;
size_t in_len = 14 * 4;
size_t out_len = 14 * 4;
size_t result = iconv(conv, &inptr, &in_len, &outptr, &out_len);
uint16_t *encoded = reinterpret_cast<uint16_t*>(outbuf);
for(size_t ii = 0; ii < 15; ii++) {
cout << setw(2) << hex << encoded[ii] << " ";
}
cout << endl;
}
输出:
48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 2e a 0
feff 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 2e a
清楚地显示结果字符串开头的BOM,其中
不存在答案 0 :(得分:1)
我发现这对我有用:
iconv_t conv = iconv_open("UTF16LE", "UTF32");
虽然我认为它依赖于实现。
有一个类似的问题:Using iconv to convert from UTF-16BE to UTF-8 without BOM