Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iconv:从UTF-16LE到UTF-8的转换在字符串的中间终止_C_Iconv - Fatal编程技术网

iconv:从UTF-16LE到UTF-8的转换在字符串的中间终止

iconv:从UTF-16LE到UTF-8的转换在字符串的中间终止,c,iconv,C,Iconv,我有一个UTF-16LE字符串“TEST”及其十六进制转储,如下所示 feff 0074 0065 0073 0074 000a 如果我在bash上使用命令iconv将这个字符串转换为UTF-8,那么它的转换就没有任何问题 657473000A 但是,如果我对我的C程序执行相同的操作,那么一旦遇到字符“T”的0x00,iconv函数似乎会将其视为空终止,即使我已将字符串长度指定为12(包括bom和空终止) 65000A 下面是我正在测试的代码。但是,如果我转换任意大小的宽字符字符串(中间没有0x

我有一个UTF-16LE字符串“TEST”及其十六进制转储,如下所示

feff 0074 0065 0073 0074 000a

如果我在bash上使用命令iconv将这个字符串转换为UTF-8,那么它的转换就没有任何问题

657473000A

但是,如果我对我的C程序执行相同的操作,那么一旦遇到字符“T”的0x00,iconv函数似乎会将其视为空终止,即使我已将字符串长度指定为12(包括bom和空终止)

65000A

下面是我正在测试的代码。但是,如果我转换任意大小的宽字符字符串(中间没有0x00字节),将返回正确的输出

char *cOutput;    // Output buffer with more enough size required
size_t tOutput; 
char *cInput;     // string wide characters
size_t tInput;
iconv_t cd;

........

cd = iconv_open("UTF8//TRANSLIT", "UTF-16LE");
iconv(cd, &cInput, &tInput, &cOutput, &tOutput);

如果我做错了什么,这个问题有什么解决办法吗?任何输入都将受到欢迎。

猜测一下,您的问题是您初始化的
tInput
不正确,可能是使用了
strlen(cInput)

此代码为我生成预期的输出:

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

int main()
{
    char utf16le_str[] = { '\xff', '\xfe', '\x74', '\x00', '\x65', '\x00',
        '\x73', '\x00', '\x74', '\x00', '\x0a', '\x00' };
    char dest_str[100];
    char *in = utf16le_str;
    char *out = dest_str;
    size_t inbytes = sizeof utf16le_str;
    size_t outbytes = sizeof dest_str;
    iconv_t conv = iconv_open("UTF-8//TRANSLIT", "UTF-16LE");

    if (conv == (iconv_t)-1) {
        perror("iconv_open");
        return 1;
    }

    if (iconv(conv, &in, &inbytes, &out, &outbytes) == (size_t)-1) {
        perror("iconv");
        return 1;
    }

    dest_str[sizeof dest_str - outbytes] = 0;
    puts(dest_str);

    return 0;
}
#包括
#包括
#包括
int main()
{
字符utf16le_str[]={'\xff','\xfe','\x74','\x00','\x65','\x00',,
'\x73'、'\x00'、'\x74'、'\x00'、'\x0a'、'\x00'};
char dest_str[100];
char*in=utf16le_str;
char*out=dest_str;
size_t inbytes=utf16le_str的大小;
size\u t outbytes=dest\u str的大小;
iconv_t conv=iconv_open(“UTF-8//translatit”,“UTF-16LE”);
如果(conv==(iconv_t)-1){
佩罗尔(“iconv_开放”);
返回1;
}
if(iconv(conv、&in、&inbytes、&out、&outbytes)==(size\u t)-1){
佩罗尔(“iconv”);
返回1;
}
dest_str[sizeof dest_str-outbytes]=0;
看跌期权(dest_str);
返回0;
}

一行代码胜过1000字。显示您的代码。正如H2CO3所说,显示更多的代码,比如如何初始化数据和大小。