1

如何在不添加字节顺序标记的情况下使 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,而它在

4

1 回答 1

1

我发现这对我有用:

iconv_t conv = iconv_open("UTF16LE", "UTF32");

虽然我相信它依赖于实现。

有一个类似的问题:Using iconv to convert from UTF-16BE to UTF-8 without BOM

于 2014-09-09T07:25:47.633 回答