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 be16toh , 32.Nm be32toh , 33.Nm be64toh , 34.Nm htobe16 , 35.Nm htobe32 , 36.Nm htobe64 , 37.Nm htole16 , 38.Nm htole32 , 39.Nm htole64 , 40.Nm le16toh , 41.Nm le32toh , 42.Nm le64toh 43.Nd convert values between big endian, little endian, and host byte order 44.Pp 45.Nm {be,le}{16,32,64}{enc,dec} 46.Nd encode and decode big or little endian values to and from byte strings 47.Sh SYNOPSIS 48.In sys/endian.h 49.Ft uint16_t 50.Fn bswap16 "uint16_t int16" 51.Ft uint32_t 52.Fn bswap32 "uint32_t int32" 53.Ft uint64_t 54.Fn bswap64 "uint64_t int64" 55.Ft uint16_t 56.Fn be16toh "uint16_t big16" 57.Ft uint32_t 58.Fn be32toh "uint32_t big32" 59.Ft uint64_t 60.Fn be64toh "uint64_t big64" 61.Ft uint16_t 62.Fn htobe16 "uint16_t host16" 63.Ft uint32_t 64.Fn htobe32 "uint32_t host32" 65.Ft uint64_t 66.Fn htobe64 "uint64_t host64" 67.Ft uint16_t 68.Fn htole16 "uint16_t host16" 69.Ft uint32_t 70.Fn htole32 "uint32_t host32" 71.Ft uint64_t 72.Fn htole64 "uint64_t host64" 73.Ft uint16_t 74.Fn le16toh "uint16_t little16" 75.Ft uint32_t 76.Fn le32toh "uint32_t little32" 77.Ft uint64_t 78.Fn le64toh "uint64_t little64" 79.Ft uint16_t 80.Fn be16dec "const void *" 81.Ft uint32_t 82.Fn be32dec "const void *" 83.Ft uint64_t 84.Fn be64dec "const void *" 85.Ft uint16_t 86.Fn le16dec "const void *" 87.Ft uint32_t 88.Fn le32dec "const void *" 89.Ft uint64_t 90.Fn le64dec "const void *" 91.Ft void 92.Fn be16enc "void *, uint16_t" 93.Ft void 94.Fn be32enc "void *, uint32_t" 95.Ft void 96.Fn be64enc "void *, uint64_t" 97.Ft void 98.Fn le16enc "void *, uint16_t" 99.Ft void 100.Fn le32enc "void *, uint32_t" 101.Ft void 102.Fn le64enc "void *, uint64_t" 103.Sh DESCRIPTION 104The 105.Fn bswap16 , 106.Fn bswap32 , 107and 108.Fn bswap64 109functions return a byte order swapped integer. 110On big endian systems, the number is converted to little endian byte order. 111On little endian systems, the number is converted to big endian byte order. 112.Pp 113The 114.Fn be16toh , 115.Fn be32toh , 116and 117.Fn be64toh 118functions return a big endian byte ordered integer 119converted to the system's native byte order. 120The return value will be the same as the argument on big endian systems. 121.Pp 122The 123.Fn le16toh , 124.Fn le32toh , 125and 126.Fn le64toh 127functions return a little endian byte ordered integer 128converted to the system's native byte order. 129The return value will be the same as the argument on little endian systems. 130.Pp 131The 132.Fn htobe16 , 133.Fn htobe32 , 134and 135.Fn htobe64 136functions return a integer in the system's native 137byte order converted to big endian byte order. 138The return value will be the same as the argument on big endian systems. 139.Pp 140The 141.Fn htole16 , 142.Fn htole32 , 143and 144.Fn htole64 145functions return a integer in the system's native 146byte order converted to little endian byte order. 147The return value will be the same as the argument on little endian systems. 148.Pp 149The 150.Fn {be,le}{16,32,64}{enc,dec} 151functions encode and decode integers to/from byte strings on any alignment 152in big/little endian format. 153.Sh SEE ALSO 154.Xr byteorder 3 155.Sh HISTORY 156The hto* and toh* functions first appeared in 157.Fx 5.0 , 158and were originally developed by the 159.Nx 160project. 161.Pp 162The 163.Fn {be,le}{16,32,64}{enc,dec} 164Functions first appeared in 165.Fx 5.1 . 166 167