如何从CP866字节获取字符串

时间:2016-02-08 08:32:10

标签: c++ encoding character-encoding

我的程序从文件读取字节并尝试将其转换为字符串。文件头是CP866中的文本。

    iconv_t cd = iconv_open("UTF-8","CP866");
    char* iconv_in = bytes.data(); //Bytes is a char vector
    char* iconv_out = (char *)malloc(counter * sizeof(char)); //counter is a length of bytes array (char vector)
    size_t iconv_in_bytes =counter;
    size_t iconv_out_bytes = counter;

    size_t ret = iconv(cd, &iconv_in, &iconv_in_bytes, &iconv_out, &iconv_out_bytes);

    if ((size_t) -1 == ret) {
            cout << "Error convert";
            return NULL;
    }

程序以失败结束(错误转换)。而这种方式并不简单而美丽。有更简单的解决方案吗?

0 个答案:

没有答案