免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4050 | 回复: 5
打印 上一主题 下一主题

请教GTK2与Windows程序通讯时出现乱码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-03 17:04 |只看该作者 |倒序浏览
有一款GUI消息通讯软件,其windows版使用MFC编写,其Linux版本为gtk1编写不支持中文消息,现在我用GTK2进行Linux版的开发。
     两台计算机A (windows) 和B (Linux),A机器中运行该消息通讯软件的windows版本,我在B机器上用GTK2编程,从控件获取到了输入的中文字符串,经过socket传送到A机器,在A机器中发现发现中文部分为乱码。查了一些资料了解到从GTK的输入控件中获取到的数据为UTF-8编码,windows从95以后开始使用Unicode,是不是需要转化一下编码?UTF-8和Unicode两者的区别是?
     附:B机器环境  locale  为 zh_CN.GBK

论坛徽章:
0
2 [报告]
发表于 2007-04-03 19:22 |只看该作者
你把B机器上的代码的编码格式转成UTF-8和Unicode各试一次
肯定是编码的原因

论坛徽章:
0
3 [报告]
发表于 2007-04-03 19:33 |只看该作者

论坛徽章:
0
4 [报告]
发表于 2007-04-03 20:14 |只看该作者
原帖由 zhongfangqing 于 2007-4-3 19:22 发表
你把B机器上的代码的编码格式转成UTF-8和Unicode各试一次
肯定是编码的原因


为了和Windows版本的程序进行中文通讯,代码全部用iconv转化成UTF8保存的.
B机器中从GTK的TextView控件获取到用户输入的中文字符串直接就从内存中socket发到A机器了,中间并没有保存在文件系统上.

论坛徽章:
0
5 [报告]
发表于 2007-04-03 20:34 |只看该作者
原帖由 whyglinux 于 2007-4-3 19:33 发表
http://bbs.chinaunix.net/viewthread.php?tid=906229


long1280 网友使用g_convert(srcStr,-1,"UTF-8","GB2312",NULL,NULL,NULL)解决了网络程序中,gtk界面显示windows传过来的中文信息的问题.从DevHelp里查到了g_convert函数的声明:

  1. gchar*      g_convert                       (const gchar *str,
  2.                                              gssize len,
  3.                                              const gchar *to_codeset,
  4.                                              const gchar *from_codeset,
  5.                                              gsize *bytes_read,
  6.                                              gsize *bytes_written,
  7.                                              GError **error);

  8. Converts a string from one character set to another.

  9. Note that you should use g_iconv() for streaming conversions[2].

  10. str :         the string to convert
  11. len :         the length of the string, or -1 if the string is nul-terminated[1].
  12. to_codeset :         name of character set into which to convert str
  13. from_codeset :         character set of str.
  14. bytes_read :         location to store the number of bytes in the input string that were successfully converted, or NULL. Even if the conversion was successful, this may be less than len if there were partial characters at the end of the input. If the error G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value stored will the byte offset after the last valid input sequence.
  15. bytes_written :         the number of bytes stored in the output buffer (not including the terminating nul).
  16. error :         location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertError may occur.
  17. Returns :         If the conversion was successful, a newly allocated nul-terminated string, which must be freed with g_free(). Otherwise NULL and error will be set.
复制代码


看样子long1280网友是将windows传过来的字符串从gb2312转换成UTF-8才正常显示到了GTK控件上.

推理:我应该把从GTK控件获取到的中文字符串从UTF-8转化成gb2312才能正常显示到windows程序里.

由此我联想到我挂载windows分区时的mount参数:
mount -t ntfs -o iocharset=gb2312 /dev/hda* /mnt/D
iocharset=gb2312指定字符集为gb2312,这样挂过来的windows分区中文文件名才不会出现乱码.可我收集到的资料显示Windows  2000及 XP均使用了Unicode,在这里我有一些不解.中文版的Windows使用gb2312吗?

现在在家里,只有一台机器,没办法测试,明天上班的时候试试g_convert(srcStr,-1,"GB2312","UTF-8",NULL,NULL,NULL)把GTK控件的字符串从UTF-8转换成GB2312发送到A机器的Windows版的程序看看可行性如何.

[ 本帖最后由 hunbuso 于 2007-4-3 20:42 编辑 ]

评分

参与人数 1可用积分 +3 收起 理由
whyglinux + 3 我很赞同

查看全部评分

论坛徽章:
0
6 [报告]
发表于 2007-04-04 08:57 |只看该作者
今天测试已经通过了,在用g_convert(srcStr,-1,"GB2312","UTF-8",NULL,NULL,NULL)把GTK控件的字符串从UTF-8转换成GB2312后,发送到A机器上的Windows版程序,顺利地显示中文了。感谢版主whyglinux的帮助还有zhongfangqing (构造快乐)的热心回帖。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP