我用

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
谁能告诉我原因以及解决方法?

要转换的代码是

"保单验证"


谢谢

最佳答案

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

我已经使用此代码测试了这种情况,它似乎可以工作:

#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编码

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

08-06 18:03