1ENDIAN(3)                  Linux Programmer's Manual                 ENDIAN(3)
2
3
4

NAME

6       htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh,
7       htobe64, htole64, be64toh, le64toh - convert values  between  host  and
8       big-/little-endian byte order
9

SYNOPSIS

11       #include <endian.h>
12
13       uint16_t htobe16(uint16_t host_16bits);
14       uint16_t htole16(uint16_t host_16bits);
15       uint16_t be16toh(uint16_t big_endian_16bits);
16       uint16_t le16toh(uint16_t little_endian_16bits);
17
18       uint32_t htobe32(uint32_t host_32bits);
19       uint32_t htole32(uint32_t host_32bits);
20       uint32_t be32toh(uint32_t big_endian_32bits);
21       uint32_t le32toh(uint32_t little_endian_32bits);
22
23       uint64_t htobe64(uint64_t host_64bits);
24       uint64_t htole64(uint64_t host_64bits);
25       uint64_t be64toh(uint64_t big_endian_64bits);
26       uint64_t le64toh(uint64_t little_endian_64bits);
27
28   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
29
30       htobe16(),   htole16(),  be16toh(),  le16toh(),  htobe32(),  htole32(),
31       be32toh(), le32toh(), htobe64(), htole64(), be64toh(), le64toh():
32           Since glibc 2.19:
33               _DEFAULT_SOURCE
34           In glibc up to and including 2.19:
35               _BSD_SOURCE
36

DESCRIPTION

38       These functions convert the byte encoding of integer  values  from  the
39       byte  order that the current CPU (the "host") uses, to and from little-
40       endian and big-endian byte order.
41
42       The number, nn, in the name of each  function  indicates  the  size  of
43       integer handled by the function, either 16, 32, or 64 bits.
44
45       The  functions  with names of the form "htobenn" convert from host byte
46       order to big-endian order.
47
48       The functions with names of the form "htolenn" convert from  host  byte
49       order to little-endian order.
50
51       The  functions with names of the form "benntoh" convert from big-endian
52       order to host byte order.
53
54       The functions with names of the form  "lenntoh"  convert  from  little-
55       endian order to host byte order.
56

VERSIONS

58       These functions were added to glibc in version 2.9.
59

CONFORMING TO

61       These  functions are nonstandard.  Similar functions are present on the
62       BSDs, where the required  header  file  is  <sys/endian.h>  instead  of
63       <endian.h>.  Unfortunately, NetBSD, FreeBSD, and glibc haven't followed
64       the original OpenBSD naming convention for these functions, whereby the
65       nn  component always appears at the end of the function name (thus, for
66       example, in NetBSD, FreeBSD, and  glibc,  the  equivalent  of  OpenBSDs
67       "betoh32" is "be32toh").
68

NOTES

70       These  functions  are similar to the older byteorder(3) family of func‐
71       tions.  For example, be32toh() is identical to ntohl().
72
73       The advantage of the byteorder(3) functions is that they  are  standard
74       functions  available  on all UNIX systems.  On the other hand, the fact
75       that they were designed for use in the context  of  TCP/IP  means  that
76       they lack the 64-bit and little-endian variants described in this page.
77

EXAMPLE

79       The  program  below  display  the results of converting an integer from
80       host byte order to both little-endian and big-endian byte order.  Since
81       host  byte  order  is  either  little-endian or big-endian, only one of
82       these conversions will have an effect.  When we run this program  on  a
83       little-endian system such as x86-32, we see the following:
84
85           $ ./a.out
86           x.u32 = 0x44332211
87           htole32(x.u32) = 0x44332211
88           htobe32(x.u32) = 0x11223344
89
90   Program source
91
92       #include <endian.h>
93       #include <stdint.h>
94       #include <stdio.h>
95       #include <stdlib.h>
96
97       int
98       main(int argc, char *argv[])
99       {
100           union {
101               uint32_t u32;
102               uint8_t arr[4];
103           } x;
104
105           x.arr[0] = 0x11;     /* Lowest-address byte */
106           x.arr[1] = 0x22;
107           x.arr[2] = 0x33;
108           x.arr[3] = 0x44;     /* Highest-address byte */
109
110           printf("x.u32 = 0x%x\n", x.u32);
111           printf("htole32(x.u32) = 0x%x\n", htole32(x.u32));
112           printf("htobe32(x.u32) = 0x%x\n", htobe32(x.u32));
113
114           exit(EXIT_SUCCESS);
115       }
116

SEE ALSO

118       bswap(3), byteorder(3)
119

COLOPHON

121       This  page  is  part of release 5.02 of the Linux man-pages project.  A
122       description of the project, information about reporting bugs,  and  the
123       latest     version     of     this    page,    can    be    found    at
124       https://www.kernel.org/doc/man-pages/.
125
126
127
128GNU                               2019-03-06                         ENDIAN(3)
Impressum