1.\" Copyright (c) 2002 Mike Barcroft <mike@FreeBSD.org> 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23.\" SUCH DAMAGE. 24.\" 25.\" $FreeBSD$ 26.\" 27.Dd April 29, 2002 28.Dt BYTEORDER 9 29.Os 30.Sh NAME 31.Nm bswap16 , bswap32 , bswap64 , 32.Nm be16toh , be32toh , be64toh , htobe16 , htobe32 , htobe64 , 33.Nm htole16 , htole32 , htole64 , le16toh , le32toh , le64toh , 34.Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec , 35.Nm le16enc , le16dec , le32enc , le32dec , le64enc , le64dec 36.Nd byte order operations 37.Sh SYNOPSIS 38.In sys/endian.h 39.Ft uint16_t 40.Fn bswap16 "uint16_t int16" 41.Ft uint32_t 42.Fn bswap32 "uint32_t int32" 43.Ft uint64_t 44.Fn bswap64 "uint64_t int64" 45.Ft uint16_t 46.Fn be16toh "uint16_t big16" 47.Ft uint32_t 48.Fn be32toh "uint32_t big32" 49.Ft uint64_t 50.Fn be64toh "uint64_t big64" 51.Ft uint16_t 52.Fn htobe16 "uint16_t host16" 53.Ft uint32_t 54.Fn htobe32 "uint32_t host32" 55.Ft uint64_t 56.Fn htobe64 "uint64_t host64" 57.Ft uint16_t 58.Fn htole16 "uint16_t host16" 59.Ft uint32_t 60.Fn htole32 "uint32_t host32" 61.Ft uint64_t 62.Fn htole64 "uint64_t host64" 63.Ft uint16_t 64.Fn le16toh "uint16_t little16" 65.Ft uint32_t 66.Fn le32toh "uint32_t little32" 67.Ft uint64_t 68.Fn le64toh "uint64_t little64" 69.Ft uint16_t 70.Fn be16dec "const void *" 71.Ft uint32_t 72.Fn be32dec "const void *" 73.Ft uint64_t 74.Fn be64dec "const void *" 75.Ft uint16_t 76.Fn le16dec "const void *" 77.Ft uint32_t 78.Fn le32dec "const void *" 79.Ft uint64_t 80.Fn le64dec "const void *" 81.Ft void 82.Fn be16enc "void *" uint16_t 83.Ft void 84.Fn be32enc "void *" uint32_t 85.Ft void 86.Fn be64enc "void *" uint64_t 87.Ft void 88.Fn le16enc "void *" uint16_t 89.Ft void 90.Fn le32enc "void *" uint32_t 91.Ft void 92.Fn le64enc "void *" uint64_t 93.Sh DESCRIPTION 94The 95.Fn bswap16 , 96.Fn bswap32 , 97and 98.Fn bswap64 99functions return a byte order swapped integer. 100On big endian systems, the number is converted to little endian byte order. 101On little endian systems, the number is converted to big endian byte order. 102.Pp 103The 104.Fn be16toh , 105.Fn be32toh , 106and 107.Fn be64toh 108functions return a big endian byte ordered integer 109converted to the system's native byte order. 110The return value will be the same as the argument on big endian systems. 111.Pp 112The 113.Fn le16toh , 114.Fn le32toh , 115and 116.Fn le64toh 117functions return a little endian byte ordered integer 118converted to the system's native byte order. 119The return value will be the same as the argument on little endian systems. 120.Pp 121The 122.Fn htobe16 , 123.Fn htobe32 , 124and 125.Fn htobe64 126functions return a integer in the system's native 127byte order converted to big endian byte order. 128The return value will be the same as the argument on big endian systems. 129.Pp 130The 131.Fn htole16 , 132.Fn htole32 , 133and 134.Fn htole64 135functions return a integer in the system's native 136byte order converted to little endian byte order. 137The return value will be the same as the argument on little endian systems. 138.Pp 139The 140.Fn be16enc , 141.Fn be16dec , 142.Fn be32enc , 143.Fn be32dec , 144.Fn be64enc , 145.Fn be64dec , 146.Fn le16enc , 147.Fn le16dec , 148.Fn le32enc , 149.Fn le32dec , 150.Fn le64enc , 151and 152.Fn le64dec 153functions encode and decode integers to/from byte strings on any alignment 154in big/little endian format. 155.Sh SEE ALSO 156.Xr byteorder 3 157.Sh HISTORY 158The 159.Fn hto* 160and 161.Fn toh* 162functions first appeared in 163.Fx 5.0 , 164and were originally developed by the 165.Nx 166project. 167.Pp 168The encode/decode functions first appeared in 169.Fx 5.1 . 170