📄 glib-character-set-conversion.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Character Set Conversion</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-utilities.html" title="GLib Utilities"><link rel="prev" href="glib-String-Utility-Functions.html" title="String Utility Functions"><link rel="next" href="glib-Unicode-Manipulation.html" title="Unicode Manipulation"><meta name="generator" content="GTK-Doc V1.9 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-String-Utility-Functions.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-utilities.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Unicode-Manipulation.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr><tr><td colspan="5" class="shortcuts"><nobr><a href="#id2988553" class="shortcut">Top</a>  |  <a href="#id2989335" class="shortcut">Description</a></nobr></td></tr></table><div class="refentry" lang="en"><a name="glib-Character-Set-Conversion"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><a name="id2988553"></a><span class="refentrytitle">Character Set Conversion</span></h2><p>Character Set Conversion — convert strings between different character sets using <code class="function">iconv()</code></p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h><a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-convert">g_convert</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *str, <a class="link" href="glib-Basic-Types.html#gssize">gssize</a> len, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *to_codeset, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *from_codeset, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-convert-with-fallback">g_convert_with_fallback</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *str, <a class="link" href="glib-Basic-Types.html#gssize">gssize</a> len, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *to_codeset, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *from_codeset, <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *fallback, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error); <a class="link" href="glib-Character-Set-Conversion.html#GIConv">GIConv</a>;<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-convert-with-iconv">g_convert_with_iconv</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *str, <a class="link" href="glib-Basic-Types.html#gssize">gssize</a> len, <a class="link" href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> converter, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);#define <a class="link" href="glib-Character-Set-Conversion.html#G-CONVERT-ERROR:CAPS">G_CONVERT_ERROR</a><a class="link" href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> <a class="link" href="glib-Character-Set-Conversion.html#g-iconv-open">g_iconv_open</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *to_codeset, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *from_codeset);<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> <a class="link" href="glib-Character-Set-Conversion.html#g-iconv">g_iconv</a> (<a class="link" href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> converter, <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> **inbuf, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *inbytes_left, <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> **outbuf, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *outbytes_left);<a class="link" href="glib-Basic-Types.html#gint">gint</a> <a class="link" href="glib-Character-Set-Conversion.html#g-iconv-close">g_iconv_close</a> (<a class="link" href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> converter);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-locale-to-utf8">g_locale_to_utf8</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *opsysstring, <a class="link" href="glib-Basic-Types.html#gssize">gssize</a> len, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-filename-to-utf8">g_filename_to_utf8</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *opsysstring, <a class="link" href="glib-Basic-Types.html#gssize">gssize</a> len, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-filename-from-utf8">g_filename_from_utf8</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *utf8string, <a class="link" href="glib-Basic-Types.html#gssize">gssize</a> len, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-filename-from-uri">g_filename_from_uri</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *uri, <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> **hostname, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-filename-to-uri">g_filename_to_uri</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *filename, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *hostname, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Character-Set-Conversion.html#g-get-filename-charsets">g_get_filename_charsets</a> (G_CONST_RETURN <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> ***charsets);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-filename-display-name">g_filename_display_name</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *filename);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-filename-display-basename">g_filename_display_basename</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *filename);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>** <a class="link" href="glib-Character-Set-Conversion.html#g-uri-list-extract-uris">g_uri_list_extract_uris</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *uri_list);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Character-Set-Conversion.html#g-locale-from-utf8">g_locale_from_utf8</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *utf8string, <a class="link" href="glib-Basic-Types.html#gssize">gssize</a> len, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a class="link" href="glib-Error-Reporting.html#GError">GError</a> **error);enum <a class="link" href="glib-Character-Set-Conversion.html#GConvertError">GConvertError</a>;<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Character-Set-Conversion.html#g-get-charset">g_get_charset</a> (G_CONST_RETURN char **charset);</pre></div><div class="refsect1" lang="en"><a name="id2989335"></a><h2>Description</h2><p></p><div class="refsect2" lang="en"><a name="file-name-encodings"></a><h3>File Name Encodings</h3><p> Historically, Unix has not had a defined encoding for file names: a file name is valid as long as it does not have path separators in it ("/"). However, displaying file names may require conversion: from the character set in which they were created, to the character set in which the application operates. Consider the Spanish file name "<code class="filename">Presentación.sxi</code>". If the application which created it uses ISO-8859-1 for its encoding, then the actual file name on disk would look like this: </p><a name="filename-iso8859-1"></a><pre class="programlisting">Character: P r e s e n t a c i ó n . s x iHex code: 50 72 65 73 65 6e 74 61 63 69 f3 6e 2e 73 78 69 </pre><p> However, if the application use UTF-8, the actual file name on disk would look like this: </p><a name="filename-utf-8"></a><pre class="programlisting">Character: P r e s e n t a c i ó n . s x iHex code: 50 72 65 73 65 6e 74 61 63 69 c3 b3 6e 2e 73 78 69 </pre><p> Glib uses UTF-8 for its strings, and GUI toolkits like GTK+ that use Glib do the same thing. If you get a file name from the file system, for example, from <code class="function">readdir(3)</code> or from <code class="function"><a class="link" href="glib-File-Utilities.html#g-dir-read-name"><code class="function">g_dir_read_name()</code></a></code>, and you wish to display the file name to the user, you <span class="emphasis"><em>will</em></span> need to convert it into UTF-8. The opposite case is when the user types the name of a file he wishes to save: the toolkit will give you that string in UTF-8 encoding, and you will need to convert it to the character set used for file names before you can create the file with <code class="function">open(2)</code> or <code class="function">fopen(3)</code>. </p><p> By default, Glib assumes that file names on disk are in UTF-8 encoding. This is a valid assumption for file systems which were created relatively recently: most applications use UTF-8 encoding for their strings, and that is also what they use for the file names they create. However, older file systems may still contain file names created in "older" encodings, such as ISO-8859-1. In this case, for compatibility reasons, you may want to instruct Glib to use that particular encoding for file names rather than UTF-8. You can do this by specifying the encoding for file names in the <a class="link" href="glib-running.html#G_FILENAME_ENCODING" title="G_FILENAME_ENCODING"><code class="envar">G_FILENAME_ENCODING</code></a> environment variable. For example, if your installation uses ISO-8859-1 for file names, you can put this in your <code class="filename">~/.profile</code>: </p><pre class="programlisting">export G_FILENAME_ENCODING=ISO-8859-1 </pre><p> Glib provides the functions <code class="function"><a class="link" href="glib-Character-Set-Conversion.html#g-filename-to-utf8"><code class="function">g_filename_to_utf8()</code></a></code> and <code class="function"><a class="link" href="glib-Character-Set-Conversion.html#g-filename-from-utf8"><code class="function">g_filename_from_utf8()</code></a></code> to perform the necessary conversions. These functions convert file names from the encoding specified in <code class="envar">G_FILENAME_ENCODING</code> to UTF-8 and vice-versa. <a class="xref" href="glib-Character-Set-Conversion.html#file-name-encodings-diagram" title="Figure 2. Conversion between File Name Encodings">Figure 2, “Conversion between File Name Encodings”</a> illustrates how these functions are used to convert between UTF-8 and the encoding for file names in the file system. </p><div class="figure"><a name="file-name-encodings-diagram"></a><p class="title"><b>Figure 2. Conversion between File Name Encodings</b></p><div class="figure-contents"><div><img src="file-name-encodings.png" alt="Conversion between File Name Encodings"></div></div></div><br class="figure-break"><div class="refsect3" lang="en"><a name="file-name-encodings-checklist"></a><h4>Checklist for Application Writers</h4><p> This section is a practical summary of the detailed description above. You can use this as a checklist of things to do to make sure your applications process file name encodings correctly. </p><div class="orderedlist"><ol type="1"><li><p> If you get a file name from the file system from a function such as <code class="function">readdir(3)</code> or <code class="function"><ahref="/usr/share/gtk-doc/html/gtk/GtkFileChooser.html#gtk-file-chooser-get-filename"><code class="function">gtk_file_chooser_get_filename()</code></a></code>, you do not need to do any conversion to pass that file name to functions like <code class="function">open(2)</code>, <code class="function">rename(2)</code>, or
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -