为什么iconv读取的字节比我指定的多

时间:2011-06-01 09:52:35

标签: linux unicode

我用

size_t iconv(iconv_t cd,   char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);

将UTF-16BE转换为GB2312。

inbytesleft是要转换的字节数。转换后,inbytesleft是未转换的字节数。

一次通话后,我发现inbytesleft为-2,根据iconv man page,此功能最多应为inbytesleft。 谁可以告诉我为什么以及如何解决这个问题?

要转换的代码是

"保单验证"

由于

1 个答案:

答案 0 :(得分:0)

如何将输入数据输入程序?

我已经使用此代码测试了这种情况,似乎有效:

#include <stdio.h>
#include <iconv.h>
#include <errno.h>

int main(){
    char data[10] = {0x4f,0xdd,0x53,0x55,0x9a,0x8c,0x8b,0xc1, 0, 0};
    char outdata[20];
    char *dataptr;
    char *outdataptr;
    iconv_t cd;
    size_t result;
    size_t inbytesleft = 8;
    size_t outbytesleft = 20;
    int i;

    cd = iconv_open("GB2312", "UTF-16BE");
    dataptr = data;
    outdataptr = outdata;
    result = iconv(cd, &dataptr, &inbytesleft, &outdataptr, &outbytesleft);
    if(result == -1)
        printf("Error: %d\n", errno);
    printf("      result: %zd\n", result);
    printf(" inbytesleft: %zd\n", inbytesleft);
    printf("outbytesleft: %zd\n", outbytesleft);

    for(i = 20; i > outbytesleft; i--){
        if(i != 20)
            printf(",");
        printf("0x%02x", *((unsigned char *)&(outdata[20-i])));
    }
    printf("\n");

    return 0;
}

打印

      result: 0
 inbytesleft: 0
outbytesleft: 12
0xb1,0xa3,0xb5,0xa5,0xd1,0xe9,0xd6,0xa4

这似乎是正确的。

变量数据中的项目数组是保单验证

的UTF-16BE编码

如果这没有帮助,您可以发布您的代码进行分析吗?