xref: /freebsd/share/man/man9/byteorder.9 (revision 923d84c0bcd6253a158ec4f56b8ac44fb53a7b0a)
18a1791edSMike Barcroft.\" Copyright (c) 2002 Mike Barcroft <mike@FreeBSD.org>
28a1791edSMike Barcroft.\" All rights reserved.
38a1791edSMike Barcroft.\"
48a1791edSMike Barcroft.\" Redistribution and use in source and binary forms, with or without
58a1791edSMike Barcroft.\" modification, are permitted provided that the following conditions
68a1791edSMike Barcroft.\" are met:
78a1791edSMike Barcroft.\" 1. Redistributions of source code must retain the above copyright
88a1791edSMike Barcroft.\"    notice, this list of conditions and the following disclaimer.
98a1791edSMike Barcroft.\" 2. Redistributions in binary form must reproduce the above copyright
108a1791edSMike Barcroft.\"    notice, this list of conditions and the following disclaimer in the
118a1791edSMike Barcroft.\"    documentation and/or other materials provided with the distribution.
128a1791edSMike Barcroft.\"
138a1791edSMike Barcroft.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
148a1791edSMike Barcroft.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158a1791edSMike Barcroft.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
168a1791edSMike Barcroft.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
178a1791edSMike Barcroft.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
188a1791edSMike Barcroft.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
198a1791edSMike Barcroft.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
208a1791edSMike Barcroft.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
218a1791edSMike Barcroft.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
228a1791edSMike Barcroft.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
238a1791edSMike Barcroft.\" SUCH DAMAGE.
248a1791edSMike Barcroft.\"
258a1791edSMike Barcroft.\" $FreeBSD$
268a1791edSMike Barcroft.\"
273f4655deSMike Barcroft.Dd April 29, 2002
288a1791edSMike Barcroft.Dt BYTEORDER 9
298a1791edSMike Barcroft.Os
308a1791edSMike Barcroft.Sh NAME
31923d84c0SRuslan Ermilov.Nm be16toh , be32toh , be64toh , htobe16 , htobe32 , htobe64 ,
32923d84c0SRuslan Ermilov.Nm htole16 , htole32 , htole64 , le16toh , le32toh , le64toh
338a1791edSMike Barcroft.Nd convert values between big endian, little endian, and host byte order
34923d84c0SRuslan Ermilov.\" XXX the following breaks whatis(1)
3521ee4ab6SPoul-Henning Kamp.Pp
36923d84c0SRuslan Ermilov.Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec ,
37923d84c0SRuslan Ermilov.Nm le16enc , le16dec , le32enc , le32dec , le64enc , le64dec
3821ee4ab6SPoul-Henning Kamp.Nd encode and decode big or little endian values to and from byte strings
398a1791edSMike Barcroft.Sh SYNOPSIS
403f4655deSMike Barcroft.In sys/endian.h
413f4655deSMike Barcroft.Ft uint16_t
423f4655deSMike Barcroft.Fn bswap16 "uint16_t int16"
433f4655deSMike Barcroft.Ft uint32_t
443f4655deSMike Barcroft.Fn bswap32 "uint32_t int32"
453f4655deSMike Barcroft.Ft uint64_t
463f4655deSMike Barcroft.Fn bswap64 "uint64_t int64"
478a1791edSMike Barcroft.Ft uint16_t
488a1791edSMike Barcroft.Fn be16toh "uint16_t big16"
498a1791edSMike Barcroft.Ft uint32_t
508a1791edSMike Barcroft.Fn be32toh "uint32_t big32"
518a1791edSMike Barcroft.Ft uint64_t
528a1791edSMike Barcroft.Fn be64toh "uint64_t big64"
538a1791edSMike Barcroft.Ft uint16_t
548a1791edSMike Barcroft.Fn htobe16 "uint16_t host16"
558a1791edSMike Barcroft.Ft uint32_t
568a1791edSMike Barcroft.Fn htobe32 "uint32_t host32"
578a1791edSMike Barcroft.Ft uint64_t
588a1791edSMike Barcroft.Fn htobe64 "uint64_t host64"
598a1791edSMike Barcroft.Ft uint16_t
608a1791edSMike Barcroft.Fn htole16 "uint16_t host16"
618a1791edSMike Barcroft.Ft uint32_t
628a1791edSMike Barcroft.Fn htole32 "uint32_t host32"
638a1791edSMike Barcroft.Ft uint64_t
648a1791edSMike Barcroft.Fn htole64 "uint64_t host64"
658a1791edSMike Barcroft.Ft uint16_t
668a1791edSMike Barcroft.Fn le16toh "uint16_t little16"
678a1791edSMike Barcroft.Ft uint32_t
688a1791edSMike Barcroft.Fn le32toh "uint32_t little32"
698a1791edSMike Barcroft.Ft uint64_t
708a1791edSMike Barcroft.Fn le64toh "uint64_t little64"
7121ee4ab6SPoul-Henning Kamp.Ft uint16_t
7221ee4ab6SPoul-Henning Kamp.Fn be16dec "const void *"
7321ee4ab6SPoul-Henning Kamp.Ft uint32_t
7421ee4ab6SPoul-Henning Kamp.Fn be32dec "const void *"
7521ee4ab6SPoul-Henning Kamp.Ft uint64_t
7621ee4ab6SPoul-Henning Kamp.Fn be64dec "const void *"
7721ee4ab6SPoul-Henning Kamp.Ft uint16_t
7821ee4ab6SPoul-Henning Kamp.Fn le16dec "const void *"
7921ee4ab6SPoul-Henning Kamp.Ft uint32_t
8021ee4ab6SPoul-Henning Kamp.Fn le32dec "const void *"
8121ee4ab6SPoul-Henning Kamp.Ft uint64_t
8221ee4ab6SPoul-Henning Kamp.Fn le64dec "const void *"
8321ee4ab6SPoul-Henning Kamp.Ft void
84923d84c0SRuslan Ermilov.Fn be16enc "void *" uint16_t
8521ee4ab6SPoul-Henning Kamp.Ft void
86923d84c0SRuslan Ermilov.Fn be32enc "void *" uint32_t
8721ee4ab6SPoul-Henning Kamp.Ft void
88923d84c0SRuslan Ermilov.Fn be64enc "void *" uint64_t
8921ee4ab6SPoul-Henning Kamp.Ft void
90923d84c0SRuslan Ermilov.Fn le16enc "void *" uint16_t
9121ee4ab6SPoul-Henning Kamp.Ft void
92923d84c0SRuslan Ermilov.Fn le32enc "void *" uint32_t
9321ee4ab6SPoul-Henning Kamp.Ft void
94923d84c0SRuslan Ermilov.Fn le64enc "void *" uint64_t
958a1791edSMike Barcroft.Sh DESCRIPTION
968a1791edSMike BarcroftThe
973f4655deSMike Barcroft.Fn bswap16 ,
983f4655deSMike Barcroft.Fn bswap32 ,
993f4655deSMike Barcroftand
1003f4655deSMike Barcroft.Fn bswap64
1013f4655deSMike Barcroftfunctions return a byte order swapped integer.
1023f4655deSMike BarcroftOn big endian systems, the number is converted to little endian byte order.
1033f4655deSMike BarcroftOn little endian systems, the number is converted to big endian byte order.
1043f4655deSMike Barcroft.Pp
1053f4655deSMike BarcroftThe
1068a1791edSMike Barcroft.Fn be16toh ,
1078a1791edSMike Barcroft.Fn be32toh ,
1088a1791edSMike Barcroftand
1098a1791edSMike Barcroft.Fn be64toh
1108a1791edSMike Barcroftfunctions return a big endian byte ordered integer
1118a1791edSMike Barcroftconverted to the system's native byte order.
1128a1791edSMike BarcroftThe return value will be the same as the argument on big endian systems.
1138a1791edSMike Barcroft.Pp
1148a1791edSMike BarcroftThe
1158a1791edSMike Barcroft.Fn le16toh ,
1168a1791edSMike Barcroft.Fn le32toh ,
1178a1791edSMike Barcroftand
1188a1791edSMike Barcroft.Fn le64toh
1198a1791edSMike Barcroftfunctions return a little endian byte ordered integer
1208a1791edSMike Barcroftconverted to the system's native byte order.
1218a1791edSMike BarcroftThe return value will be the same as the argument on little endian systems.
1228a1791edSMike Barcroft.Pp
1238a1791edSMike BarcroftThe
1248a1791edSMike Barcroft.Fn htobe16 ,
1258a1791edSMike Barcroft.Fn htobe32 ,
1268a1791edSMike Barcroftand
1278a1791edSMike Barcroft.Fn htobe64
1288a1791edSMike Barcroftfunctions return a integer in the system's native
1298a1791edSMike Barcroftbyte order converted to big endian byte order.
1308a1791edSMike BarcroftThe return value will be the same as the argument on big endian systems.
1318a1791edSMike Barcroft.Pp
1328a1791edSMike BarcroftThe
1338a1791edSMike Barcroft.Fn htole16 ,
1348a1791edSMike Barcroft.Fn htole32 ,
1358a1791edSMike Barcroftand
1368a1791edSMike Barcroft.Fn htole64
1378a1791edSMike Barcroftfunctions return a integer in the system's native
1388a1791edSMike Barcroftbyte order converted to little endian byte order.
1398a1791edSMike BarcroftThe return value will be the same as the argument on little endian systems.
14021ee4ab6SPoul-Henning Kamp.Pp
14121ee4ab6SPoul-Henning KampThe
142923d84c0SRuslan Ermilov.Fn be16enc ,
143923d84c0SRuslan Ermilov.Fn be16dec ,
144923d84c0SRuslan Ermilov.Fn be32enc ,
145923d84c0SRuslan Ermilov.Fn be32dec ,
146923d84c0SRuslan Ermilov.Fn be64enc ,
147923d84c0SRuslan Ermilov.Fn be64dec ,
148923d84c0SRuslan Ermilov.Fn le16enc ,
149923d84c0SRuslan Ermilov.Fn le16dec ,
150923d84c0SRuslan Ermilov.Fn le32enc ,
151923d84c0SRuslan Ermilov.Fn le32dec ,
152923d84c0SRuslan Ermilov.Fn le64enc ,
153923d84c0SRuslan Ermilovand
154923d84c0SRuslan Ermilov.Fn le64dec
15521ee4ab6SPoul-Henning Kampfunctions encode and decode integers to/from byte strings on any alignment
15621ee4ab6SPoul-Henning Kampin big/little endian format.
1578a1791edSMike Barcroft.Sh SEE ALSO
1588a1791edSMike Barcroft.Xr byteorder 3
1598a1791edSMike Barcroft.Sh HISTORY
160923d84c0SRuslan ErmilovThe
161923d84c0SRuslan Ermilov.Fn hto*
162923d84c0SRuslan Ermilovand
163923d84c0SRuslan Ermilov.Fn toh*
164923d84c0SRuslan Ermilovfunctions first appeared in
1658a1791edSMike Barcroft.Fx 5.0 ,
1668a1791edSMike Barcroftand were originally developed by the
1678a1791edSMike Barcroft.Nx
1688a1791edSMike Barcroftproject.
16921ee4ab6SPoul-Henning Kamp.Pp
170923d84c0SRuslan ErmilovThe encode/decode functions first appeared in
17121ee4ab6SPoul-Henning Kamp.Fx 5.1 .
172