malloc.3: ffix
[man-pages.git] / man3 / endian.3
blob6c257a2bf16bacf26db511733648ca96c240afbd
1 .\" Copyright (C) 2009, Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\" a few pieces remain from an earlier version
4 .\" Copyright (C) 2008, Nanno Langstraat <nal@ii.nl>
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
28 .TH ENDIAN 3  2021-03-22 "GNU" "Linux Programmer's Manual"
29 .SH NAME
30 htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh,
31 htobe64, htole64, be64toh, le64toh \-
32 convert values between host and big-/little-endian byte order
33 .SH SYNOPSIS
34 .nf
35 .B #include <endian.h>
36 .PP
37 .BI "uint16_t htobe16(uint16_t " host_16bits );
38 .BI "uint16_t htole16(uint16_t " host_16bits );
39 .BI "uint16_t be16toh(uint16_t " big_endian_16bits );
40 .BI "uint16_t le16toh(uint16_t " little_endian_16bits );
41 .PP
42 .BI "uint32_t htobe32(uint32_t " host_32bits );
43 .BI "uint32_t htole32(uint32_t " host_32bits );
44 .BI "uint32_t be32toh(uint32_t " big_endian_32bits );
45 .BI "uint32_t le32toh(uint32_t " little_endian_32bits );
46 .PP
47 .BI "uint64_t htobe64(uint64_t " host_64bits );
48 .BI "uint64_t htole64(uint64_t " host_64bits );
49 .BI "uint64_t be64toh(uint64_t " big_endian_64bits );
50 .BI "uint64_t le64toh(uint64_t " little_endian_64bits );
51 .fi
52 .PP
53 .RS -4
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
56 .RE
57 .ad l
58 .PP
59 .BR htobe16 (),
60 .BR htole16 (),
61 .BR be16toh (),
62 .BR le16toh (),
63 .BR htobe32 (),
64 .BR htole32 (),
65 .BR be32toh (),
66 .BR le32toh (),
67 .BR htobe64 (),
68 .BR htole64 (),
69 .BR be64toh (),
70 .BR le64toh ():
71 .nf
72     Since glibc 2.19:
73         _DEFAULT_SOURCE
74     In glibc up to and including 2.19:
75         _BSD_SOURCE
76 .fi
77 .ad
78 .SH DESCRIPTION
79 These functions convert the byte encoding of integer values from
80 the byte order that the current CPU (the "host") uses,
81 to and from little-endian and big-endian byte order.
82 .PP
83 The number,
84 .IR nn ,
85 in the name of each function indicates the size of
86 integer handled by the function, either 16, 32, or 64 bits.
87 .PP
88 The functions with names of the form "htobe\fInn\fP" convert
89 from host byte order to big-endian order.
90 .PP
91 The functions with names of the form "htole\fInn\fP" convert
92 from host byte order to little-endian order.
93 .PP
94 The functions with names of the form "be\fInn\fPtoh" convert
95 from big-endian order to host byte order.
96 .PP
97 The functions with names of the form "le\fInn\fPtoh" convert
98 from little-endian order to host byte order.
99 .SH VERSIONS
100 These functions were added to glibc in version 2.9.
101 .SH CONFORMING TO
102 These functions are nonstandard.
103 Similar functions are present on the BSDs,
104 where the required header file is
105 .I <sys/endian.h>
106 instead of
107 .IR <endian.h> .
108 Unfortunately,
109 NetBSD, FreeBSD, and glibc haven't followed the original
110 OpenBSD naming convention for these functions,
111 whereby the
112 .I nn
113 component always appears at the end of the function name
114 (thus, for example, in NetBSD, FreeBSD, and glibc,
115 the equivalent of OpenBSDs "betoh32" is "be32toh").
116 .SH NOTES
117 These functions are similar to the older
118 .BR byteorder (3)
119 family of functions.
120 For example,
121 .BR be32toh ()
122 is identical to
123 .BR ntohl ().
125 The advantage of the
126 .BR byteorder (3)
127 functions is that they are standard functions available
128 on all UNIX systems.
129 On the other hand, the fact that they were designed
130 for use in the context of TCP/IP means that
131 they lack the 64-bit and little-endian variants described in this page.
132 .SH EXAMPLES
133 The program below display the results of converting an integer
134 from host byte order to both little-endian and big-endian byte order.
135 Since host byte order is either little-endian or big-endian,
136 only one of these conversions will have an effect.
137 When we run this program on a little-endian system such as x86-32,
138 we see the following:
140 .in +4n
142 $ \fB./a.out\fP
143 x.u32 = 0x44332211
144 htole32(x.u32) = 0x44332211
145 htobe32(x.u32) = 0x11223344
148 .SS Program source
151 #include <endian.h>
152 #include <stdint.h>
153 #include <stdio.h>
154 #include <stdlib.h>
157 main(int argc, char *argv[])
159     union {
160         uint32_t u32;
161         uint8_t arr[4];
162     } x;
164     x.arr[0] = 0x11;    /* Lowest\-address byte */
165     x.arr[1] = 0x22;
166     x.arr[2] = 0x33;
167     x.arr[3] = 0x44;    /* Highest\-address byte */
169     printf("x.u32 = %#x\en", x.u32);
170     printf("htole32(x.u32) = %#x\en", htole32(x.u32));
171     printf("htobe32(x.u32) = %#x\en", htobe32(x.u32));
173     exit(EXIT_SUCCESS);
176 .SH SEE ALSO
177 .BR bswap (3),
178 .BR byteorder (3)