C++ Linux 使用 iconv 将 UTF8 转为 UTF16
我正在与在 Windows 上运行的供应商合作,并且需要 UTF16 格式的文本。我在 Linux 上运行,它是 UTF8。我正在尝试使用 iconv 从 UTF8 转换为 UTF16,以便我可以通过套接字连接发送。我在这里找到一篇文章stackOverflow,并尝试遵循该代码,但我的函数没有返回任何内容。有人有从 UTF-8 转换为 UTF-16 的经验吗?谢谢。
char * Convert( char *from_charset, char *to_charset, char *input )
{
size_t input_size, output_size, bytes_converted;
char * output;
char * tmp;
iconv_t cd;
cd = iconv_open( to_charset, from_charset);
if ( cd == (iconv_t) -1 )
{
//Something went wrong with iconv_open
if (errno == EINVAL)
{
char * buffer;
sprintf(buffer, "Conversion from %s to %s not available", from_charset, to_charset);
cout << buffer << endl;
}
return NULL;
}
input_size = strlen(input);
output_size = 2 * input_size;
output = (char*) malloc(output_size+1);
bytes_converted = iconv(cd, &input, &input_size, &output, &output_size);
cout << "Bytes converted: " << bytes_converted << endl;
if ( iconv_close (cd) != 0)
cout<< "Error closing iconv_error" << endl;
return output;
}
I am working with a vendor that is running on Windows and expects text in UTF16. I'm running on Linux and it's UTF8. I'm trying to use iconv to convert from UTF8 to UTF16 so I can send over a socket connection. I found an article here stackOverflow, and tried to follow that code but nothing is returned from my function. Has anyone had any experience converting from UTF-8 to UTF-16? thanks.
char * Convert( char *from_charset, char *to_charset, char *input )
{
size_t input_size, output_size, bytes_converted;
char * output;
char * tmp;
iconv_t cd;
cd = iconv_open( to_charset, from_charset);
if ( cd == (iconv_t) -1 )
{
//Something went wrong with iconv_open
if (errno == EINVAL)
{
char * buffer;
sprintf(buffer, "Conversion from %s to %s not available", from_charset, to_charset);
cout << buffer << endl;
}
return NULL;
}
input_size = strlen(input);
output_size = 2 * input_size;
output = (char*) malloc(output_size+1);
bytes_converted = iconv(cd, &input, &input_size, &output, &output_size);
cout << "Bytes converted: " << bytes_converted << endl;
if ( iconv_close (cd) != 0)
cout<< "Error closing iconv_error" << endl;
return output;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
iconv 函数获取字符串指针的地址,并在转换时指向 char 数组的下一个元素。当它完成时,输出实际上指向数组的最后一个元素,它可以为 null 或其他值。
您需要将输出字符串的起始地址保存在临时位置。
在将输出提供给 iconv 之前执行以下操作
char * tempoutput = 输出;
然后在 iconv 调用后返回 tempoutput;
并且不要忘记释放(输出)
并且返回的字符串也是持久的,您需要在使用转换后释放它。
the iconv function takes the address of the pointers to strings and as it s converting it points to the next element of the char array. when it finish the output actually points to the last element of the array , it can be null or something else.
you need to save the starting address of the output string in a temp location .
before feeding output to iconv do the following
char * tempoutput = output ;
and then after iconv call return tempoutput;
and dont forget to free(output)
and the returned string is also presistent you need to free it after using convert.