📄 glib-character-set-conversion.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><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.69.0"><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.4 (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"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" 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></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><span class="refentrytitle">Character Set Conversion</span></h2><p>Character Set Conversion — convert strings between different character sets using <code class="function"><code class="function">iconv()</code></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 href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-convert">g_convert</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *str, <a href="glib-Basic-Types.html#gssize">gssize</a> len, const <a href="glib-Basic-Types.html#gchar">gchar</a> *to_codeset, const <a href="glib-Basic-Types.html#gchar">gchar</a> *from_codeset, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-convert-with-fallback">g_convert_with_fallback</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *str, <a href="glib-Basic-Types.html#gssize">gssize</a> len, const <a href="glib-Basic-Types.html#gchar">gchar</a> *to_codeset, const <a href="glib-Basic-Types.html#gchar">gchar</a> *from_codeset, <a href="glib-Basic-Types.html#gchar">gchar</a> *fallback, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error); <a href="glib-Character-Set-Conversion.html#GIConv">GIConv</a>;<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-convert-with-iconv">g_convert_with_iconv</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *str, <a href="glib-Basic-Types.html#gssize">gssize</a> len, <a href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> converter, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);#define <a href="glib-Character-Set-Conversion.html#G-CONVERT-ERROR:CAPS">G_CONVERT_ERROR</a><a href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> <a href="glib-Character-Set-Conversion.html#g-iconv-open">g_iconv_open</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *to_codeset, const <a href="glib-Basic-Types.html#gchar">gchar</a> *from_codeset);size_t <a href="glib-Character-Set-Conversion.html#g-iconv">g_iconv</a> (<a href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> converter, <a href="glib-Basic-Types.html#gchar">gchar</a> **inbuf, <a href="glib-Basic-Types.html#gsize">gsize</a> *inbytes_left, <a href="glib-Basic-Types.html#gchar">gchar</a> **outbuf, <a href="glib-Basic-Types.html#gsize">gsize</a> *outbytes_left);<a href="glib-Basic-Types.html#gint">gint</a> <a href="glib-Character-Set-Conversion.html#g-iconv-close">g_iconv_close</a> (<a href="glib-Character-Set-Conversion.html#GIConv">GIConv</a> converter);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-locale-to-utf8">g_locale_to_utf8</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *opsysstring, <a href="glib-Basic-Types.html#gssize">gssize</a> len, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-filename-to-utf8">g_filename_to_utf8</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *opsysstring, <a href="glib-Basic-Types.html#gssize">gssize</a> len, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-filename-from-utf8">g_filename_from_utf8</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *utf8string, <a href="glib-Basic-Types.html#gssize">gssize</a> len, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-filename-from-uri">g_filename_from_uri</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *uri, <a href="glib-Basic-Types.html#gchar">gchar</a> **hostname, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-filename-to-uri">g_filename_to_uri</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, const <a href="glib-Basic-Types.html#gchar">gchar</a> *hostname, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Character-Set-Conversion.html#g-get-filename-charsets">g_get_filename_charsets</a> (G_CONST_RETURN <a href="glib-Basic-Types.html#gchar">gchar</a> ***charsets);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-filename-display-name">g_filename_display_name</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-filename-display-basename">g_filename_display_basename</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename);<a href="glib-Basic-Types.html#gchar">gchar</a>** <a href="glib-Character-Set-Conversion.html#g-uri-list-extract-uris">g_uri_list_extract_uris</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *uri_list);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Character-Set-Conversion.html#g-locale-from-utf8">g_locale_from_utf8</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *utf8string, <a href="glib-Basic-Types.html#gssize">gssize</a> len, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);enum <a href="glib-Character-Set-Conversion.html#GConvertError">GConvertError</a>;<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a 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="id2961123"></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 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 href="glib-running.html#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 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 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 href="glib-Character-Set-Conversion.html#file-name-encodings-diagram" title="Figure 1. Conversion between File Name Encodings">Figure 1, “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 1. Conversion between File Name Encodings</b></p><div><img src="file-name-encodings.png" alt="Conversion between File Name Encodings"></div></div><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="../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 <code class="function">fopen(3)</code> — those are "raw" file names which the file system understands. </p></li><li><p> If you need to display a file name, convert it to UTF-8 first by using <code class="function"><a href="glib-Character-Set-Conversion.html#g-filename-to-utf8"><code class="function">g_filename_to_utf8()</code></a></code>. If conversion fails, display a string like "<code class="literal">Unknown file name</code>". <span class="emphasis"><em>Do not</em></span> convert this string back into the encoding used for file names if you wish to pass it to the file system; use the original file name instead. For example, the document window of a word processor could display "Unknown file name" in its title bar but still let the user save the file, as it would keep the raw file name internally. This can happen if the user has not set the <code class="envar">G_FILENAME_ENCODING</code> environment variable even though he has files whose names are not encoded in UTF-8. </p></li><li><p> If your user interface lets the user type a file name for saving or renaming, convert it to the encoding used for file names in the file system by using <code class="function"><a href="glib-Character-Set-Conversion.html#g-filename-from-utf8"><code class="function">g_filename_from_utf8()</code></a></code>. Pass the converted file name to functions like <code class="function">fopen(3)</code>. If conversion fails, ask the user to enter a different file name. This can happen if the user types Japanese characters when <code class="envar">G_FILENAME_ENCODING</code> is set to <code class="literal">ISO-8859-1</code>, for example. </p></li></ol></div></div></div></div><div class="refsect1" lang="en"><a name="id2961558"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2961564"></a><h3><a name="g-convert"></a>g_convert ()</h3><a class="indexterm" name="id2961575"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gchar">gchar</a>* g_convert (const <a href="glib-Basic-Types.html#gchar">gchar</a> *str, <a href="glib-Basic-Types.html#gssize">gssize</a> len, const <a href="glib-Basic-Types.html#gchar">gchar</a> *to_codeset, const <a href="glib-Basic-Types.html#gchar">gchar</a> *from_codeset, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);</pre><p>Converts a string from one character set to another.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -