man-pages/man3/endian.3.html
2021-03-31 01:06:50 +01:00

246 lines
6.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of ENDIAN</TITLE>
</HEAD><BODY>
<H1>ENDIAN</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh,
htobe64, htole64, be64toh, le64toh -
convert values between host and big-/little-endian byte order
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/endian.h">endian.h</A>&gt;</B>
<B>uint16_t htobe16(uint16_t </B><I>host_16bits</I><B>);</B>
<B>uint16_t htole16(uint16_t </B><I>host_16bits</I><B>);</B>
<B>uint16_t be16toh(uint16_t </B><I>big_endian_16bits</I><B>);</B>
<B>uint16_t le16toh(uint16_t </B><I>little_endian_16bits</I><B>);</B>
<B>uint32_t htobe32(uint32_t </B><I>host_32bits</I><B>);</B>
<B>uint32_t htole32(uint32_t </B><I>host_32bits</I><B>);</B>
<B>uint32_t be32toh(uint32_t </B><I>big_endian_32bits</I><B>);</B>
<B>uint32_t le32toh(uint32_t </B><I>little_endian_32bits</I><B>);</B>
<B>uint64_t htobe64(uint64_t </B><I>host_64bits</I><B>);</B>
<B>uint64_t htole64(uint64_t </B><I>host_64bits</I><B>);</B>
<B>uint64_t be64toh(uint64_t </B><I>big_endian_64bits</I><B>);</B>
<B>uint64_t le64toh(uint64_t </B><I>little_endian_64bits</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>htobe16</B>(),
<B>htole16</B>(),
<B>be16toh</B>(),
<B>le16toh</B>(),
<B>htobe32</B>(),
<B>htole32</B>(),
<B>be32toh</B>(),
<B>le32toh</B>(),
<B>htobe64</B>(),
<B>htole64</B>(),
<B>be64toh</B>(),
<B>le64toh</B>():
<PRE>
Since glibc 2.19:
_DEFAULT_SOURCE
In glibc up to and including 2.19:
_BSD_SOURCE
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
These functions convert the byte encoding of integer values from
the byte order that the current CPU (the &quot;host&quot;) uses,
to and from little-endian and big-endian byte order.
<P>
The number,
<I>nn</I>,
in the name of each function indicates the size of
integer handled by the function, either 16, 32, or 64 bits.
<P>
The functions with names of the form &quot;htobe<I>nn</I>&quot; convert
from host byte order to big-endian order.
<P>
The functions with names of the form &quot;htole<I>nn</I>&quot; convert
from host byte order to little-endian order.
<P>
The functions with names of the form &quot;be<I>nn</I>toh&quot; convert
from big-endian order to host byte order.
<P>
The functions with names of the form &quot;le<I>nn</I>toh&quot; convert
from little-endian order to host byte order.
<A NAME="lbAE">&nbsp;</A>
<H2>VERSIONS</H2>
These functions were added to glibc in version 2.9.
<A NAME="lbAF">&nbsp;</A>
<H2>CONFORMING TO</H2>
These functions are nonstandard.
Similar functions are present on the BSDs,
where the required header file is
<I>&lt;<A HREF="file:///usr/include/sys/endian.h">sys/endian.h</A>&gt;</I>
instead of
<I>&lt;<A HREF="file:///usr/include/endian.h">endian.h</A>&gt;</I>.
Unfortunately,
NetBSD, FreeBSD, and glibc haven't followed the original
OpenBSD naming convention for these functions,
whereby the
<I>nn</I>
component always appears at the end of the function name
(thus, for example, in NetBSD, FreeBSD, and glibc,
the equivalent of OpenBSDs &quot;betoh32&quot; is &quot;be32toh&quot;).
<A NAME="lbAG">&nbsp;</A>
<H2>NOTES</H2>
These functions are similar to the older
<B><A HREF="/cgi-bin/man/man2html?3+byteorder">byteorder</A></B>(3)
family of functions.
For example,
<B>be32toh</B>()
is identical to
<B>ntohl</B>().
<P>
The advantage of the
<B><A HREF="/cgi-bin/man/man2html?3+byteorder">byteorder</A></B>(3)
functions is that they are standard functions available
on all UNIX systems.
On the other hand, the fact that they were designed
for use in the context of TCP/IP means that
they lack the 64-bit and little-endian variants described in this page.
<A NAME="lbAH">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below display the results of converting an integer
from host byte order to both little-endian and big-endian byte order.
Since host byte order is either little-endian or big-endian,
only one of these conversions will have an effect.
When we run this program on a little-endian system such as x86-32,
we see the following:
<P>
$ <B>./a.out</B>
x.u32 = 0x44332211
htole32(x.u32) = 0x44332211
htobe32(x.u32) = 0x11223344
<A NAME="lbAI">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/endian.h">endian.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdint.h">stdint.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;union&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uint32_t&nbsp;u32;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uint8_t&nbsp;arr[4];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;x;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;x.arr[0]&nbsp;=&nbsp;0x11;<TT>&nbsp;&nbsp;&nbsp;&nbsp;</TT>/*&nbsp;Lowest-address&nbsp;byte&nbsp;*/<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;x.arr[1]&nbsp;=&nbsp;0x22;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;x.arr[2]&nbsp;=&nbsp;0x33;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;x.arr[3]&nbsp;=&nbsp;0x44;<TT>&nbsp;&nbsp;&nbsp;&nbsp;</TT>/*&nbsp;Highest-address&nbsp;byte&nbsp;*/<BR>
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;x.u32&nbsp;=&nbsp;0x%x\n&quot;,&nbsp;x.u32);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;htole32(x.u32)&nbsp;=&nbsp;0x%x\n&quot;,&nbsp;htole32(x.u32));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;htobe32(x.u32)&nbsp;=&nbsp;0x%x\n&quot;,&nbsp;htobe32(x.u32));
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+bswap">bswap</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+byteorder">byteorder</A></B>(3)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">VERSIONS</A><DD>
<DT id="5"><A HREF="#lbAF">CONFORMING TO</A><DD>
<DT id="6"><A HREF="#lbAG">NOTES</A><DD>
<DT id="7"><A HREF="#lbAH">EXAMPLE</A><DD>
<DL>
<DT id="8"><A HREF="#lbAI">Program source</A><DD>
</DL>
<DT id="9"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="10"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:39 GMT, March 31, 2021
</BODY>
</HTML>