OSDN Git Service

Update README
[linuxjm/LDP_man-pages.git] / original / man3 / endian.3
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  2010-09-10 "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 .BR "#define _BSD_SOURCE" "             /* See feature_test_macros(7) */"
36 .B #include <endian.h>
37
38 .BI "uint16_t htobe16(uint16_t " host_16bits );
39 .BI "uint16_t htole16(uint16_t " host_16bits );
40 .BI "uint16_t be16toh(uint16_t " big_endian_16bits );
41 .BI "uint16_t le16toh(uint16_t " little_endian_16bits );
42
43 .BI "uint32_t htobe32(uint32_t " host_32bits );
44 .BI "uint32_t htole32(uint32_t " host_32bits );
45 .BI "uint32_t be32toh(uint32_t " big_endian_32bits );
46 .BI "uint32_t le32toh(uint32_t " little_endian_32bits );
47
48 .BI "uint64_t htobe64(uint64_t " host_64bits );
49 .BI "uint64_t htole64(uint64_t " host_64bits );
50 .BI "uint64_t be64toh(uint64_t " big_endian_64bits );
51 .BI "uint64_t le64toh(uint64_t " little_endian_64bits );
52 .fi
53 .SH DESCRIPTION
54 These functions convert the byte encoding of integer values from
55 the byte order that the current CPU (the "host") uses,
56 to and from little-endian and big-endian byte order.
57
58 The number,
59 .IR nn ,
60 in the name of each function indicates the size of
61 integer handled by the function, either 16, 32, or 64 bits.
62
63 The functions with names of the form "htobe\fInn\fP" convert
64 from host byte order to big-endian order.
65
66 The functions with names of the form "htole\fInn\fP" convert
67 from host byte order to little-endian order.
68
69 The functions with names of the form "be\fInn\fPtoh" convert
70 from big-endian order to host byte order.
71
72 The functions with names of the form "le\fInn\fPtoh" convert
73 from little-endian order to host byte order.
74 .SH VERSIONS
75 These functions were added to glibc in version 2.9.
76 .SH CONFORMING TO
77 These functions are nonstandard.
78 Similar functions are present on the BSDs,
79 where the required header file is
80 .I <sys/endian.h>
81 instead of
82 .IR <endian.h> .
83 Unfortunately,
84 NetBSD, FreeBSD, and glibc haven't followed the original
85 OpenBSD naming convention for these functions,
86 whereby the
87 .I nn
88 component always appears at the end of the function name
89 (thus, for example, in NetBSD, FreeBSD, and glibc,
90 the equivalent of OpenBSDs "betoh32" is "be32toh").
91 .SH NOTES
92 These functions are similar to the older
93 .BR byteorder (3)
94 family of functions.
95 For example,
96 .BR be32toh ()
97 is identical to
98 .BR ntohl ().
99
100 The advantage of the
101 .BR byteorder (3)
102 functions is that they are standard functions available
103 on all UNIX systems.
104 On the other hand, the fact that they were designed
105 for use in the context of TCP/IP means that
106 they lack the 64-bit and little-endian variants described in this page.
107 .SH EXAMPLE
108 The program below display the results of converting an integer
109 from host byte order to both little-endian and big-endian byte order.
110 Since host byte order is either little-endian or big-endian,
111 only one of these conversions will have an effect.
112 When we run this program on a little-endian system such as x86-32,
113 we see the following:
114 .in +4n
115 .nf
116
117 $ \fB./a.out\fP
118 x.u32 = 0x44332211
119 htole32(x.u32) = 0x44332211
120 htobe32(x.u32) = 0x11223344
121 .fi
122 .in
123 .SS Program source
124 \&
125 .nf
126 #include <endian.h>
127 #include <stdint.h>
128 #include <stdio.h>
129 #include <stdlib.h>
130
131 int
132 main(int argc, char *argv[])
133 {
134     union {
135         uint32_t u32;
136         uint8_t arr[4];
137     } x;
138
139     x.arr[0] = 0x11;    /* Lowest-address byte */
140     x.arr[1] = 0x22;
141     x.arr[2] = 0x33;
142     x.arr[3] = 0x44;    /* Highest-address byte */
143
144     printf("x.u32 = 0x%x\\n", x.u32);
145     printf("htole32(x.u32) = 0x%x\\n", htole32(x.u32));
146     printf("htobe32(x.u32) = 0x%x\\n", htobe32(x.u32));
147
148     exit(EXIT_SUCCESS);
149 }
150 .fi
151 .SH SEE ALSO
152 .BR byteorder (3)
153 .SH COLOPHON
154 This page is part of release 3.79 of the Linux
155 .I man-pages
156 project.
157 A description of the project,
158 information about reporting bugs,
159 and the latest version of this page,
160 can be found at
161 \%http://www.kernel.org/doc/man\-pages/.