在 C 中使用 iconv API
我尝试使用 iconv API 将 sjis 字符串转换为 utf-8。我已经成功编译了它,但输出不是我所期望的。 我的代码:
void convertUtf8ToSjis(char* utf8, char* sjis){
iconv_t icd;
int index = 0;
char *p_src, *p_dst;
size_t n_src, n_dst;
icd = iconv_open("Shift_JIS", "UTF-8");
int c;
p_src = utf8;
p_dst = sjis;
n_src = strlen(utf8);
n_dst = 32; // my sjis string size
iconv(icd, &p_src, &n_src, &p_dst, &n_dst);
iconv_close(icd);
}
我只得到随机数。有什么想法吗?
编辑: 我的输入是
char utf8[] = "\xe4\xba\x9c"; //亜
输出应该是: 0x88 0x9F
但实际上是: 0x30 0x00 0x00 0x31 0x00 ...
I try to convert a sjis string to utf-8 using the iconv API. I compiled it already succesfully, but the output isn't what I expected.
My code:
void convertUtf8ToSjis(char* utf8, char* sjis){
iconv_t icd;
int index = 0;
char *p_src, *p_dst;
size_t n_src, n_dst;
icd = iconv_open("Shift_JIS", "UTF-8");
int c;
p_src = utf8;
p_dst = sjis;
n_src = strlen(utf8);
n_dst = 32; // my sjis string size
iconv(icd, &p_src, &n_src, &p_dst, &n_dst);
iconv_close(icd);
}
I got only random numbers. Any ideas?
Edit:
My input is
char utf8[] = "\xe4\xba\x9c"; //亜
And output should be:
0x88 0x9F
But is in fact:
0x30 0x00 0x00 0x31 0x00 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法重现该问题。我唯一可以建议的是要小心你的分配。
代码:
输出:
I was unable to duplicate the problem. The only thing I can suggest is to be careful about your allocations.
Code:
Output: