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