1endian(3)                  Library Functions 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

LIBRARY

11       Standard C library (libc, -lc)
12

SYNOPSIS

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

DESCRIPTION

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

VERSIONS

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

STANDARDS

69       None.
70

HISTORY

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

EXAMPLES

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

SEE ALSO

122       bswap(3), byteorder(3)
123
124
125
126Linux man-pages 6.04              2023-03-30                         endian(3)
Impressum