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.\" 253f4655deSMike Barcroft.Dd April 29, 2002 268a1791edSMike Barcroft.Dt BYTEORDER 9 278a1791edSMike Barcroft.Os 288a1791edSMike Barcroft.Sh NAME 290bf85f4fSRuslan Ermilov.Nm bswap16 , bswap32 , bswap64 , 30923d84c0SRuslan Ermilov.Nm be16toh , be32toh , be64toh , htobe16 , htobe32 , htobe64 , 310bf85f4fSRuslan Ermilov.Nm htole16 , htole32 , htole64 , le16toh , le32toh , le64toh , 32923d84c0SRuslan Ermilov.Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec , 33923d84c0SRuslan Ermilov.Nm le16enc , le16dec , le32enc , le32dec , le64enc , le64dec 340bf85f4fSRuslan Ermilov.Nd byte order operations 358a1791edSMike Barcroft.Sh SYNOPSIS 363f4655deSMike Barcroft.In sys/endian.h 373f4655deSMike Barcroft.Ft uint16_t 383f4655deSMike Barcroft.Fn bswap16 "uint16_t int16" 393f4655deSMike Barcroft.Ft uint32_t 403f4655deSMike Barcroft.Fn bswap32 "uint32_t int32" 413f4655deSMike Barcroft.Ft uint64_t 423f4655deSMike Barcroft.Fn bswap64 "uint64_t int64" 438a1791edSMike Barcroft.Ft uint16_t 448a1791edSMike Barcroft.Fn be16toh "uint16_t big16" 458a1791edSMike Barcroft.Ft uint32_t 468a1791edSMike Barcroft.Fn be32toh "uint32_t big32" 478a1791edSMike Barcroft.Ft uint64_t 488a1791edSMike Barcroft.Fn be64toh "uint64_t big64" 498a1791edSMike Barcroft.Ft uint16_t 508a1791edSMike Barcroft.Fn htobe16 "uint16_t host16" 518a1791edSMike Barcroft.Ft uint32_t 528a1791edSMike Barcroft.Fn htobe32 "uint32_t host32" 538a1791edSMike Barcroft.Ft uint64_t 548a1791edSMike Barcroft.Fn htobe64 "uint64_t host64" 558a1791edSMike Barcroft.Ft uint16_t 568a1791edSMike Barcroft.Fn htole16 "uint16_t host16" 578a1791edSMike Barcroft.Ft uint32_t 588a1791edSMike Barcroft.Fn htole32 "uint32_t host32" 598a1791edSMike Barcroft.Ft uint64_t 608a1791edSMike Barcroft.Fn htole64 "uint64_t host64" 618a1791edSMike Barcroft.Ft uint16_t 628a1791edSMike Barcroft.Fn le16toh "uint16_t little16" 638a1791edSMike Barcroft.Ft uint32_t 648a1791edSMike Barcroft.Fn le32toh "uint32_t little32" 658a1791edSMike Barcroft.Ft uint64_t 668a1791edSMike Barcroft.Fn le64toh "uint64_t little64" 6721ee4ab6SPoul-Henning Kamp.Ft uint16_t 6821ee4ab6SPoul-Henning Kamp.Fn be16dec "const void *" 6921ee4ab6SPoul-Henning Kamp.Ft uint32_t 7021ee4ab6SPoul-Henning Kamp.Fn be32dec "const void *" 7121ee4ab6SPoul-Henning Kamp.Ft uint64_t 7221ee4ab6SPoul-Henning Kamp.Fn be64dec "const void *" 7321ee4ab6SPoul-Henning Kamp.Ft uint16_t 7421ee4ab6SPoul-Henning Kamp.Fn le16dec "const void *" 7521ee4ab6SPoul-Henning Kamp.Ft uint32_t 7621ee4ab6SPoul-Henning Kamp.Fn le32dec "const void *" 7721ee4ab6SPoul-Henning Kamp.Ft uint64_t 7821ee4ab6SPoul-Henning Kamp.Fn le64dec "const void *" 7921ee4ab6SPoul-Henning Kamp.Ft void 80923d84c0SRuslan Ermilov.Fn be16enc "void *" uint16_t 8121ee4ab6SPoul-Henning Kamp.Ft void 82923d84c0SRuslan Ermilov.Fn be32enc "void *" uint32_t 8321ee4ab6SPoul-Henning Kamp.Ft void 84923d84c0SRuslan Ermilov.Fn be64enc "void *" uint64_t 8521ee4ab6SPoul-Henning Kamp.Ft void 86923d84c0SRuslan Ermilov.Fn le16enc "void *" uint16_t 8721ee4ab6SPoul-Henning Kamp.Ft void 88923d84c0SRuslan Ermilov.Fn le32enc "void *" uint32_t 8921ee4ab6SPoul-Henning Kamp.Ft void 90923d84c0SRuslan Ermilov.Fn le64enc "void *" uint64_t 918a1791edSMike Barcroft.Sh DESCRIPTION 928a1791edSMike BarcroftThe 933f4655deSMike Barcroft.Fn bswap16 , 943f4655deSMike Barcroft.Fn bswap32 , 953f4655deSMike Barcroftand 963f4655deSMike Barcroft.Fn bswap64 973f4655deSMike Barcroftfunctions return a byte order swapped integer. 983f4655deSMike BarcroftOn big endian systems, the number is converted to little endian byte order. 993f4655deSMike BarcroftOn little endian systems, the number is converted to big endian byte order. 1003f4655deSMike Barcroft.Pp 1013f4655deSMike BarcroftThe 1028a1791edSMike Barcroft.Fn be16toh , 1038a1791edSMike Barcroft.Fn be32toh , 1048a1791edSMike Barcroftand 1058a1791edSMike Barcroft.Fn be64toh 1068a1791edSMike Barcroftfunctions return a big endian byte ordered integer 1078a1791edSMike Barcroftconverted to the system's native byte order. 1088a1791edSMike BarcroftThe return value will be the same as the argument on big endian systems. 1098a1791edSMike Barcroft.Pp 1108a1791edSMike BarcroftThe 1118a1791edSMike Barcroft.Fn le16toh , 1128a1791edSMike Barcroft.Fn le32toh , 1138a1791edSMike Barcroftand 1148a1791edSMike Barcroft.Fn le64toh 1158a1791edSMike Barcroftfunctions return a little endian byte ordered integer 1168a1791edSMike Barcroftconverted to the system's native byte order. 1178a1791edSMike BarcroftThe return value will be the same as the argument on little endian systems. 1188a1791edSMike Barcroft.Pp 1198a1791edSMike BarcroftThe 1208a1791edSMike Barcroft.Fn htobe16 , 1218a1791edSMike Barcroft.Fn htobe32 , 1228a1791edSMike Barcroftand 1238a1791edSMike Barcroft.Fn htobe64 1249aae2835SKevin Lofunctions return an integer in the system's native 1258a1791edSMike Barcroftbyte order converted to big endian byte order. 1268a1791edSMike BarcroftThe return value will be the same as the argument on big endian systems. 1278a1791edSMike Barcroft.Pp 1288a1791edSMike BarcroftThe 1298a1791edSMike Barcroft.Fn htole16 , 1308a1791edSMike Barcroft.Fn htole32 , 1318a1791edSMike Barcroftand 1328a1791edSMike Barcroft.Fn htole64 1338a1791edSMike Barcroftfunctions return a integer in the system's native 1348a1791edSMike Barcroftbyte order converted to little endian byte order. 1358a1791edSMike BarcroftThe return value will be the same as the argument on little endian systems. 13621ee4ab6SPoul-Henning Kamp.Pp 13721ee4ab6SPoul-Henning KampThe 138923d84c0SRuslan Ermilov.Fn be16enc , 139923d84c0SRuslan Ermilov.Fn be16dec , 140923d84c0SRuslan Ermilov.Fn be32enc , 141923d84c0SRuslan Ermilov.Fn be32dec , 142923d84c0SRuslan Ermilov.Fn be64enc , 143923d84c0SRuslan Ermilov.Fn be64dec , 144923d84c0SRuslan Ermilov.Fn le16enc , 145923d84c0SRuslan Ermilov.Fn le16dec , 146923d84c0SRuslan Ermilov.Fn le32enc , 147923d84c0SRuslan Ermilov.Fn le32dec , 148923d84c0SRuslan Ermilov.Fn le64enc , 149923d84c0SRuslan Ermilovand 150923d84c0SRuslan Ermilov.Fn le64dec 15121ee4ab6SPoul-Henning Kampfunctions encode and decode integers to/from byte strings on any alignment 15221ee4ab6SPoul-Henning Kampin big/little endian format. 1538a1791edSMike Barcroft.Sh SEE ALSO 1548a1791edSMike Barcroft.Xr byteorder 3 1558a1791edSMike Barcroft.Sh HISTORY 156923d84c0SRuslan ErmilovThe 157923d84c0SRuslan Ermilov.Fn hto* 158923d84c0SRuslan Ermilovand 159*73bbeaa5SGlen Barber.Fn *toh 160923d84c0SRuslan Ermilovfunctions first appeared in 1618a1791edSMike Barcroft.Fx 5.0 , 1628a1791edSMike Barcroftand were originally developed by the 1638a1791edSMike Barcroft.Nx 1648a1791edSMike Barcroftproject. 16521ee4ab6SPoul-Henning Kamp.Pp 166923d84c0SRuslan ErmilovThe encode/decode functions first appeared in 16721ee4ab6SPoul-Henning Kamp.Fx 5.1 . 168