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 310bf85f4fSRuslan Ermilov.Nm bswap16 , bswap32 , bswap64 , 32923d84c0SRuslan Ermilov.Nm be16toh , be32toh , be64toh , htobe16 , htobe32 , htobe64 , 330bf85f4fSRuslan Ermilov.Nm htole16 , htole32 , htole64 , le16toh , le32toh , le64toh , 34923d84c0SRuslan Ermilov.Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec , 35923d84c0SRuslan Ermilov.Nm le16enc , le16dec , le32enc , le32dec , le64enc , le64dec 360bf85f4fSRuslan Ermilov.Nd byte order operations 378a1791edSMike Barcroft.Sh SYNOPSIS 383f4655deSMike Barcroft.In sys/endian.h 393f4655deSMike Barcroft.Ft uint16_t 403f4655deSMike Barcroft.Fn bswap16 "uint16_t int16" 413f4655deSMike Barcroft.Ft uint32_t 423f4655deSMike Barcroft.Fn bswap32 "uint32_t int32" 433f4655deSMike Barcroft.Ft uint64_t 443f4655deSMike Barcroft.Fn bswap64 "uint64_t int64" 458a1791edSMike Barcroft.Ft uint16_t 468a1791edSMike Barcroft.Fn be16toh "uint16_t big16" 478a1791edSMike Barcroft.Ft uint32_t 488a1791edSMike Barcroft.Fn be32toh "uint32_t big32" 498a1791edSMike Barcroft.Ft uint64_t 508a1791edSMike Barcroft.Fn be64toh "uint64_t big64" 518a1791edSMike Barcroft.Ft uint16_t 528a1791edSMike Barcroft.Fn htobe16 "uint16_t host16" 538a1791edSMike Barcroft.Ft uint32_t 548a1791edSMike Barcroft.Fn htobe32 "uint32_t host32" 558a1791edSMike Barcroft.Ft uint64_t 568a1791edSMike Barcroft.Fn htobe64 "uint64_t host64" 578a1791edSMike Barcroft.Ft uint16_t 588a1791edSMike Barcroft.Fn htole16 "uint16_t host16" 598a1791edSMike Barcroft.Ft uint32_t 608a1791edSMike Barcroft.Fn htole32 "uint32_t host32" 618a1791edSMike Barcroft.Ft uint64_t 628a1791edSMike Barcroft.Fn htole64 "uint64_t host64" 638a1791edSMike Barcroft.Ft uint16_t 648a1791edSMike Barcroft.Fn le16toh "uint16_t little16" 658a1791edSMike Barcroft.Ft uint32_t 668a1791edSMike Barcroft.Fn le32toh "uint32_t little32" 678a1791edSMike Barcroft.Ft uint64_t 688a1791edSMike Barcroft.Fn le64toh "uint64_t little64" 6921ee4ab6SPoul-Henning Kamp.Ft uint16_t 7021ee4ab6SPoul-Henning Kamp.Fn be16dec "const void *" 7121ee4ab6SPoul-Henning Kamp.Ft uint32_t 7221ee4ab6SPoul-Henning Kamp.Fn be32dec "const void *" 7321ee4ab6SPoul-Henning Kamp.Ft uint64_t 7421ee4ab6SPoul-Henning Kamp.Fn be64dec "const void *" 7521ee4ab6SPoul-Henning Kamp.Ft uint16_t 7621ee4ab6SPoul-Henning Kamp.Fn le16dec "const void *" 7721ee4ab6SPoul-Henning Kamp.Ft uint32_t 7821ee4ab6SPoul-Henning Kamp.Fn le32dec "const void *" 7921ee4ab6SPoul-Henning Kamp.Ft uint64_t 8021ee4ab6SPoul-Henning Kamp.Fn le64dec "const void *" 8121ee4ab6SPoul-Henning Kamp.Ft void 82923d84c0SRuslan Ermilov.Fn be16enc "void *" uint16_t 8321ee4ab6SPoul-Henning Kamp.Ft void 84923d84c0SRuslan Ermilov.Fn be32enc "void *" uint32_t 8521ee4ab6SPoul-Henning Kamp.Ft void 86923d84c0SRuslan Ermilov.Fn be64enc "void *" uint64_t 8721ee4ab6SPoul-Henning Kamp.Ft void 88923d84c0SRuslan Ermilov.Fn le16enc "void *" uint16_t 8921ee4ab6SPoul-Henning Kamp.Ft void 90923d84c0SRuslan Ermilov.Fn le32enc "void *" uint32_t 9121ee4ab6SPoul-Henning Kamp.Ft void 92923d84c0SRuslan Ermilov.Fn le64enc "void *" uint64_t 938a1791edSMike Barcroft.Sh DESCRIPTION 948a1791edSMike BarcroftThe 953f4655deSMike Barcroft.Fn bswap16 , 963f4655deSMike Barcroft.Fn bswap32 , 973f4655deSMike Barcroftand 983f4655deSMike Barcroft.Fn bswap64 993f4655deSMike Barcroftfunctions return a byte order swapped integer. 1003f4655deSMike BarcroftOn big endian systems, the number is converted to little endian byte order. 1013f4655deSMike BarcroftOn little endian systems, the number is converted to big endian byte order. 1023f4655deSMike Barcroft.Pp 1033f4655deSMike BarcroftThe 1048a1791edSMike Barcroft.Fn be16toh , 1058a1791edSMike Barcroft.Fn be32toh , 1068a1791edSMike Barcroftand 1078a1791edSMike Barcroft.Fn be64toh 1088a1791edSMike Barcroftfunctions return a big endian byte ordered integer 1098a1791edSMike Barcroftconverted to the system's native byte order. 1108a1791edSMike BarcroftThe return value will be the same as the argument on big endian systems. 1118a1791edSMike Barcroft.Pp 1128a1791edSMike BarcroftThe 1138a1791edSMike Barcroft.Fn le16toh , 1148a1791edSMike Barcroft.Fn le32toh , 1158a1791edSMike Barcroftand 1168a1791edSMike Barcroft.Fn le64toh 1178a1791edSMike Barcroftfunctions return a little endian byte ordered integer 1188a1791edSMike Barcroftconverted to the system's native byte order. 1198a1791edSMike BarcroftThe return value will be the same as the argument on little endian systems. 1208a1791edSMike Barcroft.Pp 1218a1791edSMike BarcroftThe 1228a1791edSMike Barcroft.Fn htobe16 , 1238a1791edSMike Barcroft.Fn htobe32 , 1248a1791edSMike Barcroftand 1258a1791edSMike Barcroft.Fn htobe64 1269aae2835SKevin Lofunctions return an integer in the system's native 1278a1791edSMike Barcroftbyte order converted to big endian byte order. 1288a1791edSMike BarcroftThe return value will be the same as the argument on big endian systems. 1298a1791edSMike Barcroft.Pp 1308a1791edSMike BarcroftThe 1318a1791edSMike Barcroft.Fn htole16 , 1328a1791edSMike Barcroft.Fn htole32 , 1338a1791edSMike Barcroftand 1348a1791edSMike Barcroft.Fn htole64 1358a1791edSMike Barcroftfunctions return a integer in the system's native 1368a1791edSMike Barcroftbyte order converted to little endian byte order. 1378a1791edSMike BarcroftThe return value will be the same as the argument on little endian systems. 13821ee4ab6SPoul-Henning Kamp.Pp 13921ee4ab6SPoul-Henning KampThe 140923d84c0SRuslan Ermilov.Fn be16enc , 141923d84c0SRuslan Ermilov.Fn be16dec , 142923d84c0SRuslan Ermilov.Fn be32enc , 143923d84c0SRuslan Ermilov.Fn be32dec , 144923d84c0SRuslan Ermilov.Fn be64enc , 145923d84c0SRuslan Ermilov.Fn be64dec , 146923d84c0SRuslan Ermilov.Fn le16enc , 147923d84c0SRuslan Ermilov.Fn le16dec , 148923d84c0SRuslan Ermilov.Fn le32enc , 149923d84c0SRuslan Ermilov.Fn le32dec , 150923d84c0SRuslan Ermilov.Fn le64enc , 151923d84c0SRuslan Ermilovand 152923d84c0SRuslan Ermilov.Fn le64dec 15321ee4ab6SPoul-Henning Kampfunctions encode and decode integers to/from byte strings on any alignment 15421ee4ab6SPoul-Henning Kampin big/little endian format. 1558a1791edSMike Barcroft.Sh SEE ALSO 1568a1791edSMike Barcroft.Xr byteorder 3 1578a1791edSMike Barcroft.Sh HISTORY 158923d84c0SRuslan ErmilovThe 159923d84c0SRuslan Ermilov.Fn hto* 160923d84c0SRuslan Ermilovand 161*73bbeaa5SGlen Barber.Fn *toh 162923d84c0SRuslan Ermilovfunctions first appeared in 1638a1791edSMike Barcroft.Fx 5.0 , 1648a1791edSMike Barcroftand were originally developed by the 1658a1791edSMike Barcroft.Nx 1668a1791edSMike Barcroftproject. 16721ee4ab6SPoul-Henning Kamp.Pp 168923d84c0SRuslan ErmilovThe encode/decode functions first appeared in 16921ee4ab6SPoul-Henning Kamp.Fx 5.1 . 170