c++ - Linux 上的 iconv() 32 位与 64 位

标签 c++ linux utf-8 iconv

我有一些代码在 Linux 上使用 iconv() 来验证字符串是否为 UTF-8 编码。我创建的转换如下:

iconv_t c = iconv_open("UTF-8","UTF-8");

我像这样运行 iconv():

int status = iconv(c, &fromArray, (size_t*)&inSize, &toArray, (size_t*)&outSize);

然后如果 status,我将字符串设为有效的 UTF-8不是-1。

这在 32 位环境(它最初是在其中开发和测试)中编译和工作良好。但是,我现在需要让它在 64 位环境中工作(具体来说,我相信是 Fedora 14 的 64 位版本)。当我在此处编译并运行测试时 status总是 -1我总是得到 EILSEQ errno 中的错误,即使对于 32 位编译器认为没问题的相同字符串也是如此。

有人知道为什么会发生这种情况吗?

最佳答案

最近遇到了同样的问题。转换到 (size_t*) 更(非常)可能是根本原因。 这个问题甚至可以很容易地用下一个代码模拟:

cat >Makefile <<EOF
all: build test clean

clean:
    rm -f *.o core* t32 t64
test: build
    @echo ; echo "run_32bit version:" ; ./t32
    @echo ; echo "run_64bit version:" ; ./t64
build:
    g++ -m32 t.cpp -o t32 -Wall -O0 -g
    g++      t.cpp -o t64 -Wall -O0 -g
EOF

cat >t.cpp <<EOF
#include <errno.h>
#include <stdio.h>
#include <iconv.h>

char buff_toArray [BUFSIZ];
char buff_fromArray [] = \
"<TESTS_STRINGS>\
    <T_VERIFICATION_STRINGS/>\
</TESTS_STRINGS>";

void iconv_test ( const char* desc, size_t* size )
{
   printf ("%s = size[%zu]\n",desc, (*size) );
}

int main (int argc, char* argv[])
{

 char* toArray = &buff_toArray[0];
 char* fromArray = &buff_fromArray[0];

 const int inSize_const = 61;
 short inSize_short = (short) sizeof(buff_fromArray);
 int inSize_int = (int) sizeof(buff_fromArray);
 unsigned int inSize_uint = (unsigned int) sizeof(buff_fromArray);
 long inSize_long = (long) sizeof(buff_fromArray);
 long long inSize_llong = (long long) sizeof(buff_fromArray);
 size_t inSize_size_t = sizeof(buff_fromArray);

 printf ("fake iconv usage:\n");
 iconv_test((const char*) "inSize_const", (size_t*)&inSize_const);
 iconv_test((const char*) "inSize_short", (size_t*)&inSize_short);
 iconv_test((const char*) "inSize_int", (size_t*)&inSize_int);
 iconv_test((const char*) "inSize_uint", (size_t*)&inSize_uint);
 iconv_test((const char*) "inSize_long", (size_t*)&inSize_long);
 iconv_test((const char*) "inSize_llong", (size_t*)&inSize_llong);
 iconv_test((const char*) "inSize_size_t", &inSize_size_t);

 printf ("real iconv usage:\n");
 int inSize = sizeof(buff_fromArray);
 int outSize = sizeof(buff_toArray);

 iconv_t c = iconv_open("UTF-8","UTF-8");
 int status = iconv(c, &fromArray, (size_t*)&inSize, &toArray, (size_t*)&outSize);
 printf ("status=[%d], errno=[%d] \n", status, errno );

 printf ("result string:\n");
 for(size_t i = 0; i <= sizeof(buff_toArray); i++) { printf ("%c", buff_toArray[i]); }
 printf ("\n");

 int close_status =  iconv_close(c);
 printf ("close status=[%d], errno=[%d] \n", close_status, errno );

 return 0;
}
EOF

关于c++ - Linux 上的 iconv() 32 位与 64 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16725612/

相关文章:

python - 运行 python3 manage.py migrate 时 Virtualenv(Pyt​​hon3.5/pip3/Django2.0/psycopg2) 出错

Perl 错误的 UTF-8 输出

python - 在 Windows 中单击 .lnk 时出现 Jupyter UTF-8 错误

c++ - 类型转换会减慢程序运行吗?

c++ - "invalid conversion from"与 pthread_create 问题

c++ - 实时使用opencv的光流

c++ - 我正在尝试在 Eclipse 中构建单个 C++ 项目,但即使在使用 mingw 路径设置路径变量后也会出现构建错误

linux - 编写一个shell脚本,在该行的前面的文本文件中打印每行的行号

linux - bash - 找不到 make 命令

scala - 将 Scala Dataframe 写入 CSV 文件时应用 UTF8 编码