GTK中文字符转换函数

时间:2021-04-17 06:39:00

最近在学习Gtk,遇到打开中文目录时中文字符转换问题。

下面是Glib中的本地字符转unicode的函数:

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.

 

为了方便起见写个宏:

以后用起来就方便了。不过这个宏忽略的转换中的错误。