免费注册 查看新帖 |

Chinaunix

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

g_locale_to_utf8问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-18 20:52 |只看该作者 |倒序浏览
char *animaltype = get_animal_type(tagbuf);
g_locale_to_utf8(animaltype, -1, NULL, NULL, NULL);
printf("animaltype %s\n",animaltype);   //调试时显示animaltype 为中文“山羊”(正是我想要的内容,可是却无法在编辑框内显示?)
gtk_entry_set_text(GTK_ENTRY(etyType),animaltype);
为什么编辑框无法显示animaltype?

论坛徽章:
0
2 [报告]
发表于 2009-08-21 16:52 |只看该作者
g_locale_to_utf8 ()

gchar*              g_locale_to_utf8                    (const gchar *opsysstring,
                                                         gssize len,
                                                         gsize *bytes_read,
                                                         gsize *bytes_written,
                                                         GError **error);

Converts a string which is in the encoding used for strings by the C runtime (usually the same as that used by the operating system) in the current locale into a UTF-8 string.

opsysstring :
        a string in the encoding of the current locale. On Windows this means the system codepage.

len :
        the length of the string, or -1 if the string is nul-terminated[1].

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.

bytes_written :
        the number of bytes stored in the output buffer (not including the terminating nul).

error :
        location to store the error occuring, or NULL to ignore errors. Any of the errors in GConvertError may occur.

Returns :
        The converted string, or NULL on an error.



g_locale_to_utf8函数的返回值才是utf8格式的编码,你在打印的时候是正常的,是因为打印的仍然是当前语言环境下面的编码,不是utf8的编码。

改为:
char * test = g_locale_to_utf8(animaltype, -1, NULL, NULL, NULL);
gtk_entry_set_text(GTK_ENTRY(etyType),test);

论坛徽章:
0
3 [报告]
发表于 2009-12-08 09:47 |只看该作者
楼上正解,这个函数的第一个参数const gchar类型

论坛徽章:
0
4 [报告]
发表于 2009-12-10 12:19 |只看该作者
2楼正解。另外,不要忘了释放test。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP