246 lines
6.9 KiB
HTML
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"> </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"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/endian.h">endian.h</A>></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"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
These functions convert the byte encoding of integer values from
|
|
the byte order that the current CPU (the "host") 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 "htobe<I>nn</I>" convert
|
|
from host byte order to big-endian order.
|
|
<P>
|
|
|
|
The functions with names of the form "htole<I>nn</I>" convert
|
|
from host byte order to little-endian order.
|
|
<P>
|
|
|
|
The functions with names of the form "be<I>nn</I>toh" convert
|
|
from big-endian order to host byte order.
|
|
<P>
|
|
|
|
The functions with names of the form "le<I>nn</I>toh" convert
|
|
from little-endian order to host byte order.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
These functions were added to glibc in version 2.9.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
These functions are nonstandard.
|
|
Similar functions are present on the BSDs,
|
|
where the required header file is
|
|
<I><<A HREF="file:///usr/include/sys/endian.h">sys/endian.h</A>></I>
|
|
|
|
instead of
|
|
<I><<A HREF="file:///usr/include/endian.h">endian.h</A>></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 "betoh32" is "be32toh").
|
|
<A NAME="lbAG"> </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"> </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"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/endian.h">endian.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdint.h">stdint.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> union {
|
|
<BR> uint32_t u32;
|
|
<BR> uint8_t arr[4];
|
|
<BR> } x;
|
|
<P>
|
|
<BR> x.arr[0] = 0x11;<TT> </TT>/* Lowest-address byte */<BR>
|
|
<BR> x.arr[1] = 0x22;
|
|
<BR> x.arr[2] = 0x33;
|
|
<BR> x.arr[3] = 0x44;<TT> </TT>/* Highest-address byte */<BR>
|
|
<P>
|
|
<BR> printf("x.u32 = 0x%x\n", x.u32);
|
|
<BR> printf("htole32(x.u32) = 0x%x\n", htole32(x.u32));
|
|
<BR> printf("htobe32(x.u32) = 0x%x\n", htobe32(x.u32));
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAJ"> </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"> </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"> </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>
|