📄 glib-byte-order-macros.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>Byte Order Macros</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-fundamentals.html" title="GLib Fundamentals"><link rel="prev" href="glib-Type-Conversion-Macros.html" title="Type Conversion Macros"><link rel="next" href="glib-Numerical-Definitions.html" title="Numerical Definitions"><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-Type-Conversion-Macros.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-fundamentals.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-Numerical-Definitions.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="#id2834059" class="shortcut">Top</a>  |  <a href="#id2834573" class="shortcut">Description</a></nobr></td></tr></table><div class="refentry" lang="en"><a name="glib-Byte-Order-Macros"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><a name="id2834059"></a><span class="refentrytitle">Byte Order Macros</span></h2><p>Byte Order Macros — a portable way to convert between different byte orders</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h>#define <a class="link" href="glib-Byte-Order-Macros.html#G-BYTE-ORDER:CAPS">G_BYTE_ORDER</a>#define <a class="link" href="glib-Byte-Order-Macros.html#G-LITTLE-ENDIAN:CAPS">G_LITTLE_ENDIAN</a>#define <a class="link" href="glib-Byte-Order-Macros.html#G-BIG-ENDIAN:CAPS">G_BIG_ENDIAN</a>#define <a class="link" href="glib-Byte-Order-Macros.html#G-PDP-ENDIAN:CAPS">G_PDP_ENDIAN</a>#define <a class="link" href="glib-Byte-Order-Macros.html#g-htonl">g_htonl</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#g-htons">g_htons</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#g-ntohl">g_ntohl</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#g-ntohs">g_ntohs</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT-FROM-BE:CAPS">GINT_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT-FROM-LE:CAPS">GINT_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT-TO-BE:CAPS">GINT_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT-TO-LE:CAPS">GINT_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT-FROM-BE:CAPS">GUINT_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT-FROM-LE:CAPS">GUINT_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT-TO-BE:CAPS">GUINT_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT-TO-LE:CAPS">GUINT_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GLONG-FROM-BE:CAPS">GLONG_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GLONG-FROM-LE:CAPS">GLONG_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GLONG-TO-BE:CAPS">GLONG_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GLONG-TO-LE:CAPS">GLONG_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GULONG-FROM-BE:CAPS">GULONG_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GULONG-FROM-LE:CAPS">GULONG_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GULONG-TO-BE:CAPS">GULONG_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GULONG-TO-LE:CAPS">GULONG_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT16-FROM-BE:CAPS">GINT16_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT16-FROM-LE:CAPS">GINT16_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT16-TO-BE:CAPS">GINT16_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT16-TO-LE:CAPS">GINT16_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT16-FROM-BE:CAPS">GUINT16_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT16-FROM-LE:CAPS">GUINT16_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT16-TO-BE:CAPS">GUINT16_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT16-TO-LE:CAPS">GUINT16_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT32-FROM-BE:CAPS">GINT32_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT32-FROM-LE:CAPS">GINT32_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT32-TO-BE:CAPS">GINT32_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT32-TO-LE:CAPS">GINT32_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT32-FROM-BE:CAPS">GUINT32_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT32-FROM-LE:CAPS">GUINT32_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT32-TO-BE:CAPS">GUINT32_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT32-TO-LE:CAPS">GUINT32_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT64-FROM-BE:CAPS">GINT64_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT64-FROM-LE:CAPS">GINT64_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT64-TO-BE:CAPS">GINT64_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GINT64-TO-LE:CAPS">GINT64_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT64-FROM-BE:CAPS">GUINT64_FROM_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT64-FROM-LE:CAPS">GUINT64_FROM_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT64-TO-BE:CAPS">GUINT64_TO_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT64-TO-LE:CAPS">GUINT64_TO_LE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT16-SWAP-BE-PDP:CAPS">GUINT16_SWAP_BE_PDP</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT16-SWAP-LE-BE:CAPS">GUINT16_SWAP_LE_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT16-SWAP-LE-PDP:CAPS">GUINT16_SWAP_LE_PDP</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT32-SWAP-BE-PDP:CAPS">GUINT32_SWAP_BE_PDP</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT32-SWAP-LE-BE:CAPS">GUINT32_SWAP_LE_BE</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT32-SWAP-LE-PDP:CAPS">GUINT32_SWAP_LE_PDP</a> (val)#define <a class="link" href="glib-Byte-Order-Macros.html#GUINT64-SWAP-LE-BE:CAPS">GUINT64_SWAP_LE_BE</a> (val)</pre></div><div class="refsect1" lang="en"><a name="id2834573"></a><h2>Description</h2><p>These macros provide a portable way to determine the host byte orderand to convert values between different byte orders. </p><p>The byte order is the order in which bytes are stored to create largerdata types such as the <a class="link" href="glib-Basic-Types.html#gint"><span class="type">gint</span></a> and <a class="link" href="glib-Basic-Types.html#glong"><span class="type">glong</span></a> values.The host byte order is the byte order used on the current machine.</p><p>Some processors store the most significant bytes (i.e. the bytes thathold the largest part of the value) first. These are known as big-endianprocessors. </p><p>Other processors (notably the x86 family) store the most significant bytelast. These are known as little-endian processors.</p><p>Finally, to complicate matters, some other processors store the bytes ina rather curious order known as PDP-endian. For a 4-byte word, the 3rdmost significant byte is stored first, then the 4th, then the 1st and finallythe 2nd.</p><p>Obviously there is a problem when these different processors communicatewith each other, for example over networks or by using binary file formats.This is where these macros come in.They are typically used to convert values into a byte orderwhich has been agreed on for use when communicating between differentprocessors. The Internet uses what is known as 'network byte order'as the standard byte order (which is in fact the big-endian byte order).</p><p>Note that the byte order conversion macros may evaluate their arguments multiple times, thus you should not use them with arguments which haveside-effects.</p></div><div class="refsect1" lang="en"><a name="id2834648"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2834658"></a><h3><a name="G-BYTE-ORDER:CAPS"></a>G_BYTE_ORDER</h3><a class="indexterm" name="id2834671"></a><pre class="programlisting">#define G_BYTE_ORDER G_LITTLE_ENDIAN</pre><p>The host byte order.This can be either <a class="link" href="glib-Byte-Order-Macros.html#G-LITTLE-ENDIAN:CAPS"><span class="type">G_LITTLE_ENDIAN</span></a> or <a class="link" href="glib-Byte-Order-Macros.html#G-BIG-ENDIAN:CAPS"><span class="type">G_BIG_ENDIAN</span></a> (support for<a class="link" href="glib-Byte-Order-Macros.html#G-PDP-ENDIAN:CAPS"><span class="type">G_PDP_ENDIAN</span></a> may be added in future.)</p></div><hr><div class="refsect2" lang="en"><a name="id2834713"></a><h3><a name="G-LITTLE-ENDIAN:CAPS"></a>G_LITTLE_ENDIAN</h3><a class="indexterm" name="id2834727"></a><pre class="programlisting">#define G_LITTLE_ENDIAN 1234</pre><p>Specifies one of the possible types of byte order.See <a class="link" href="glib-Byte-Order-Macros.html#G-BYTE-ORDER:CAPS"><span class="type">G_BYTE_ORDER</span></a>.</p></div><hr><div class="refsect2" lang="en"><a name="id2834751"></a><h3><a name="G-BIG-ENDIAN:CAPS"></a>G_BIG_ENDIAN</h3><a class="indexterm" name="id2834763"></a><pre class="programlisting">#define G_BIG_ENDIAN 4321</pre><p>Specifies one of the possible types of byte order.See <a class="link" href="glib-Byte-Order-Macros.html#G-BYTE-ORDER:CAPS"><span class="type">G_BYTE_ORDER</span></a>.</p></div><hr><div class="refsect2" lang="en"><a name="id2834788"></a><h3><a name="G-PDP-ENDIAN:CAPS"></a>G_PDP_ENDIAN</h3><a class="indexterm" name="id2834800"></a><pre class="programlisting">#define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */ </pre><p>Specifies one of the possible types of byte order (currently unused).See <a class="link" href="glib-Byte-Order-Macros.html#G-BYTE-ORDER:CAPS"><span class="type">G_BYTE_ORDER</span></a>.</p></div><hr><div class="refsect2" lang="en"><a name="id2834825"></a><h3><a name="g-htonl"></a>g_htonl()</h3><a class="indexterm" name="id2834837"></a><pre class="programlisting">#define g_htonl(val)</pre><p>Converts a 32-bit integer value from host to network byte order.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>val</code></em> :</span></p></td><td>a 32-bit integer value in host byte order.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td><em class="parameter"><code>val</code></em> converted to network byte order.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2834888"></a><h3><a name="g-htons"></a>g_htons()</h3><a class="indexterm" name="id2834900"></a><pre class="programlisting">#define g_htons(val)</pre><p>Converts a 16-bit integer value from host to network byte order.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>val</code></em> :</span></p></td><td>a 16-bit integer value in host byte order.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td><em class="parameter"><code>val</code></em> converted to network byte order.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2834950"></a><h3><a name="g-ntohl"></a>g_ntohl()</h3><a class="indexterm" name="id2834962"></a><pre class="programlisting">#define g_ntohl(val)</pre><p>Converts a 32-bit integer value from network to host byte order.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>val</code></em> :</span></p></td><td>a 32-bit integer value in network byte order.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td><em class="parameter"><code>val</code></em> converted to host byte order.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2835012"></a><h3><a name="g-ntohs"></a>g_ntohs()</h3><a class="indexterm" name="id2835025"></a><pre class="programlisting">#define g_ntohs(val)</pre><p>Converts a 16-bit integer value from network to host byte order.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>val</code></em> :</span></p></td><td>a 16-bit integer value in network byte order.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td><em class="parameter"><code>val</code></em> converted to host byte order.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2835075"></a><h3><a name="GINT-FROM-BE:CAPS"></a>GINT_FROM_BE()</h3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -