summaryrefslogtreecommitdiffstats
path: root/man3/endian.3
blob: 6cd85317aba1b0db61922035012231057ade7d5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
.\" Copyright (C) 2009, Linux Foundation, written by Michael Kerrisk
.\"     <mtk.manpages@gmail.com>
.\" a few pieces remain from an earlier version
.\" Copyright (C) 2008, Nanno Langstraat <nal@ii.nl>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH endian 3 (date) "Linux man-pages (unreleased)"
.SH NAME
htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh,
htobe64, htole64, be64toh, le64toh \-
convert values between host and big-/little-endian byte order
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <endian.h>
.P
.BI "uint16_t htobe16(uint16_t " host_16bits );
.BI "uint16_t htole16(uint16_t " host_16bits );
.BI "uint16_t be16toh(uint16_t " big_endian_16bits );
.BI "uint16_t le16toh(uint16_t " little_endian_16bits );
.P
.BI "uint32_t htobe32(uint32_t " host_32bits );
.BI "uint32_t htole32(uint32_t " host_32bits );
.BI "uint32_t be32toh(uint32_t " big_endian_32bits );
.BI "uint32_t le32toh(uint32_t " little_endian_32bits );
.P
.BI "uint64_t htobe64(uint64_t " host_64bits );
.BI "uint64_t htole64(uint64_t " host_64bits );
.BI "uint64_t be64toh(uint64_t " big_endian_64bits );
.BI "uint64_t le64toh(uint64_t " little_endian_64bits );
.fi
.P
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.ad l
.P
.BR htobe16 (),
.BR htole16 (),
.BR be16toh (),
.BR le16toh (),
.BR htobe32 (),
.BR htole32 (),
.BR be32toh (),
.BR le32toh (),
.BR htobe64 (),
.BR htole64 (),
.BR be64toh (),
.BR le64toh ():
.nf
    Since glibc 2.19:
        _DEFAULT_SOURCE
    In glibc up to and including 2.19:
        _BSD_SOURCE
.fi
.ad
.SH DESCRIPTION
These functions convert the byte encoding of integer values from
the byte order that the current CPU (the "host") uses,
to and from little-endian and big-endian byte order.
.P
The number,
.IR nn ,
in the name of each function indicates the size of
integer handled by the function, either 16, 32, or 64 bits.
.P
The functions with names of the form "htobe\fInn\fP" convert
from host byte order to big-endian order.
.P
The functions with names of the form "htole\fInn\fP" convert
from host byte order to little-endian order.
.P
The functions with names of the form "be\fInn\fPtoh" convert
from big-endian order to host byte order.
.P
The functions with names of the form "le\fInn\fPtoh" convert
from little-endian order to host byte order.
.SH VERSIONS
Similar functions are present on the BSDs,
where the required header file is
.I <sys/endian.h>
instead of
.IR <endian.h> .
Unfortunately,
NetBSD, FreeBSD, and glibc haven't followed the original
OpenBSD naming convention for these functions,
whereby the
.I nn
component always appears at the end of the function name
(thus, for example, in NetBSD, FreeBSD, and glibc,
the equivalent of OpenBSDs "betoh32" is "be32toh").
.SH STANDARDS
None.
.SH HISTORY
glibc 2.9.
.P
These functions are similar to the older
.BR byteorder (3)
family of functions.
For example,
.BR be32toh ()
is identical to
.BR ntohl ().
.P
The advantage of the
.BR byteorder (3)
functions is that they are standard functions available
on all UNIX systems.
On the other hand, the fact that they were designed
for use in the context of TCP/IP means that
they lack the 64-bit and little-endian variants described in this page.
.SH EXAMPLES
The program below display the results of converting an integer
from host byte order to both little-endian and big-endian byte order.
Since host byte order is either little-endian or big-endian,
only one of these conversions will have an effect.
When we run this program on a little-endian system such as x86-32,
we see the following:
.P
.in +4n
.EX
$ \fB./a.out\fP
x.u32 = 0x44332211
htole32(x.u32) = 0x44332211
htobe32(x.u32) = 0x11223344
.EE
.in
.SS Program source
\&
.\" SRC BEGIN (endian.c)
.EX
#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
\&
int
main(void)
{
    union {
        uint32_t u32;
        uint8_t arr[4];
    } x;
\&
    x.arr[0] = 0x11;	/* Lowest\-address byte */
    x.arr[1] = 0x22;
    x.arr[2] = 0x33;
    x.arr[3] = 0x44;	/* Highest\-address byte */
\&
    printf("x.u32 = %#x\en", x.u32);
    printf("htole32(x.u32) = %#x\en", htole32(x.u32));
    printf("htobe32(x.u32) = %#x\en", htobe32(x.u32));
\&
    exit(EXIT_SUCCESS);
}
.EE
.\" SRC END
.SH SEE ALSO
.BR bswap (3),
.BR byteorder (3)