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       #define _BSD_SOURCE             /* See feature_test_macros(7) */
12       #include <endian.h>
13
14       uint16_t htobe16(uint16_t host_16bits);
15       uint16_t htole16(uint16_t host_16bits);
16       uint16_t be16toh(uint16_t big_endian_16bits);
17       uint16_t le16toh(uint16_t little_endian_16bits);
18
19       uint32_t htobe32(uint32_t host_32bits);
20       uint32_t htole32(uint32_t host_32bits);
21       uint32_t be32toh(uint32_t big_endian_32bits);
22       uint32_t le32toh(uint32_t little_endian_32bits);
23
24       uint64_t htobe64(uint64_t host_64bits);
25       uint64_t htole64(uint64_t host_64bits);
26       uint64_t be64toh(uint64_t big_endian_64bits);
27       uint64_t le64toh(uint64_t little_endian_64bits);
28

DESCRIPTION

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

VERSIONS

50       These functions were added to glibc in version 2.9.
51

CONFORMING TO

53       These functions are nonstandard.  Similar functions are present on  the
54       BSDs,  where  the  required  header  file  is <sys/endian.h> instead of
55       <endian.h>.  Unfortunately, NetBSD, FreeBSD, and glibc haven't followed
56       the original OpenBSD naming convention for these functions, whereby the
57       nn component always appears at the end of the function name (thus,  for
58       example,  in  NetBSD,  FreeBSD,  and  glibc, the equivalent of OpenBSDs
59       "betoh32" is "be32toh").
60

NOTES

62       These functions are similar to the older byteorder(3) family  of  func‐
63       tions.  For example, be32toh() is identical to ntohl().
64
65       The  advantage  of the byteorder(3) functions is that they are standard
66       functions available on all UNIX systems.  On the other hand,  the  fact
67       that  they  were  designed  for use in the context of TCP/IP means that
68       they lack the 64-bit and little-endian variants described in this page.
69

EXAMPLE

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

SEE ALSO

110       byteorder(3)
111

COLOPHON

113       This page is part of release 3.53 of the Linux  man-pages  project.   A
114       description  of  the project, and information about reporting bugs, can
115       be found at http://www.kernel.org/doc/man-pages/.
116
117
118
119GNU                               2010-09-10                         ENDIAN(3)
Impressum