endian.3: New page documenting byte order conversion functions
Document functions (new in glibc 2.9) for conversion between
host byte order and big-/little- endian byte order:
htobe16(), htole16(), be16toh(), le16toh(),
htobe32(), htole32(), be32toh(), le32toh(),
htobe64(), htole64(), be64toh(), le64toh()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-01-14 10:04:12 +00:00
|
|
|
.\" Copyright (C) 2009, Linux Foundation, written by Michael Kerrisk
|
|
|
|
.\" <mtk.manpages@gmail.com>
|
|
|
|
.\" a few pieces remain from an earlier version
|
|
|
|
.\" Copyright (C) 2008, Nanno Langstraat <nal@ii.nl>
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
|
|
|
.\"
|
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
|
|
|
.\"
|
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
accept.2, clone.2, dup.2, fallocate.2, pipe.2, readahead.2, sched_setaffinity.2, unshare.2, CPU_SET.3, endian.3, euidaccess.3, fexecve.3, getpt.3, getpw.3, getumask.3, getutmp.3, gnu_get_libc_version.3, makedev.3, matherr.3, mbsnrtowcs.3, memfrob.3, pthread_attr_setaffinity_np.3, pthread_getattr_np.3, pthread_setaffinity_np.3, pthread_tryjoin_np.3, tcgetsid.3, wcscasecmp.3, wcsncasecmp.3, wcsnlen.3, wcsnrtombs.3, wcswidth.3, rtld-audit.7: SYNOPSIS: Add reference to feature_test_macros(7)
These pages specify feature test macros in the function
prototypes. Add a reference to feature_test_macros(7),
so that readers are pointed to the information that
feature test macros must be defined before including
*any* header file.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-09-10 05:06:22 +00:00
|
|
|
.TH ENDIAN 3 2010-09-10 "GNU" "Linux Programmer's Manual"
|
endian.3: New page documenting byte order conversion functions
Document functions (new in glibc 2.9) for conversion between
host byte order and big-/little- endian byte order:
htobe16(), htole16(), be16toh(), le16toh(),
htobe32(), htole32(), be32toh(), le32toh(),
htobe64(), htole64(), be64toh(), le64toh()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-01-14 10:04:12 +00:00
|
|
|
.SH NAME
|
|
|
|
htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh,
|
|
|
|
htobe64, htole64, be64toh, le64toh \-
|
|
|
|
convert values between host and big-/little-endian byte order
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
accept.2, clone.2, dup.2, fallocate.2, pipe.2, readahead.2, sched_setaffinity.2, unshare.2, CPU_SET.3, endian.3, euidaccess.3, fexecve.3, getpt.3, getpw.3, getumask.3, getutmp.3, gnu_get_libc_version.3, makedev.3, matherr.3, mbsnrtowcs.3, memfrob.3, pthread_attr_setaffinity_np.3, pthread_getattr_np.3, pthread_setaffinity_np.3, pthread_tryjoin_np.3, tcgetsid.3, wcscasecmp.3, wcsncasecmp.3, wcsnlen.3, wcsnrtombs.3, wcswidth.3, rtld-audit.7: SYNOPSIS: Add reference to feature_test_macros(7)
These pages specify feature test macros in the function
prototypes. Add a reference to feature_test_macros(7),
so that readers are pointed to the information that
feature test macros must be defined before including
*any* header file.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-09-10 05:06:22 +00:00
|
|
|
.BR "#define _BSD_SOURCE" " /* See feature_test_macros(7) */"
|
endian.3: New page documenting byte order conversion functions
Document functions (new in glibc 2.9) for conversion between
host byte order and big-/little- endian byte order:
htobe16(), htole16(), be16toh(), le16toh(),
htobe32(), htole32(), be32toh(), le32toh(),
htobe64(), htole64(), be64toh(), le64toh()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-01-14 10:04:12 +00:00
|
|
|
.B #include <endian.h>
|
|
|
|
|
|
|
|
.BI "uint16_t htobe16(uint16_t " host_16bits );
|
|
|
|
.BI "uint16_t htole16(uint16_t " host_16bits );
|
|
|
|
.BI "uint16_t be16toh(uint16_t " big_endian_16bits );
|
|
|
|
.BI "uint16_t le16toh(uint16_t " little_endian_16bits );
|
|
|
|
|
|
|
|
.BI "uint32_t htobe32(uint32_t " host_32bits );
|
|
|
|
.BI "uint32_t htole32(uint32_t " host_32bits );
|
|
|
|
.BI "uint32_t be32toh(uint32_t " big_endian_32bits );
|
|
|
|
.BI "uint32_t le32toh(uint32_t " little_endian_32bits );
|
|
|
|
|
|
|
|
.BI "uint64_t htobe64(uint64_t " host_64bits );
|
|
|
|
.BI "uint64_t htole64(uint64_t " host_64bits );
|
|
|
|
.BI "uint64_t be64toh(uint64_t " big_endian_64bits );
|
|
|
|
.BI "uint64_t le64toh(uint64_t " little_endian_64bits );
|
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
|
|
|
These functions convert the byte encoding of integer values from
|
|
|
|
the byte order that the current CPU (the "host") uses,
|
|
|
|
to and from little-endian and big-endian byte order.
|
|
|
|
|
|
|
|
The number,
|
|
|
|
.IR nn ,
|
|
|
|
in the name of each function indicates the size of
|
|
|
|
integer handled by the function, either 16, 32, or 64 bits.
|
|
|
|
|
|
|
|
The functions with names of the form "htobe\fInn\fP" convert
|
|
|
|
from host byte order to big-endian order.
|
|
|
|
|
|
|
|
The functions with names of the form "htole\fInn\fP" convert
|
|
|
|
from host byte order to little-endian order.
|
|
|
|
|
|
|
|
The functions with names of the form "be\fInn\fPtoh" convert
|
|
|
|
from big-endian order to host byte order.
|
|
|
|
|
|
|
|
The functions with names of the form "le\fInn\fPtoh" convert
|
|
|
|
from little-endian order to host byte order.
|
|
|
|
.SH VERSIONS
|
2012-05-06 17:32:47 +00:00
|
|
|
These functions were added to glibc in version 2.9.
|
endian.3: New page documenting byte order conversion functions
Document functions (new in glibc 2.9) for conversion between
host byte order and big-/little- endian byte order:
htobe16(), htole16(), be16toh(), le16toh(),
htobe32(), htole32(), be32toh(), le32toh(),
htobe64(), htole64(), be64toh(), le64toh()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-01-14 10:04:12 +00:00
|
|
|
.SH "CONFORMING TO"
|
accept.2, execve.2, futimesat.2, getresuid.2, getrlimit.2, madvise.2, mq_getsetattr.2, msgctl.2, nice.2, open.2, poll.2, setresuid.2, shmctl.2, sigaction.2, times.2, utimensat.2, __setfpucw.3, atoi.3, endian.3, err.3, euidaccess.3, fenv.3, fopencookie.3, fpurge.3, getgrouplist.3, getutmp.3, j0.3, lgamma.3, netlink.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_cleanup_push_defer_np.3, pthread_getattr_np.3, pthread_kill_other_threads_np.3, pthread_setaffinity_np.3, pthread_tryjoin_np.3, pthread_yield.3, random_r.3, readdir.3, rtnetlink.3, scanf.3, setenv.3, significand.3, sigsetops.3, strerror.3, strstr.3, sysv_signal.3, termios.3, timegm.3, unlocked_stdio.3, y0.3, locale.5, bootparam.7, feature_test_macros.7, ip.7, locale.7, pthreads.7, rtld-audit.7, nscd.8: Global fix: s/non-standard/nonstandard/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:37:52 +00:00
|
|
|
These functions are nonstandard.
|
2009-01-18 00:53:05 +00:00
|
|
|
Similar functions are present on the BSDs,
|
endian.3: New page documenting byte order conversion functions
Document functions (new in glibc 2.9) for conversion between
host byte order and big-/little- endian byte order:
htobe16(), htole16(), be16toh(), le16toh(),
htobe32(), htole32(), be32toh(), le32toh(),
htobe64(), htole64(), be64toh(), le64toh()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-01-14 10:04:12 +00:00
|
|
|
where the required header file is
|
|
|
|
.I <sys/endian.h>
|
|
|
|
instead of
|
|
|
|
.IR <endian.h> .
|
2009-01-18 00:53:05 +00:00
|
|
|
Unfortunately,
|
|
|
|
NetBSD, FreeBSD, and glibc haven't followed the original
|
|
|
|
OpenBSD naming convention for these functions,
|
|
|
|
whereby the
|
|
|
|
.I nn
|
|
|
|
component always appears at the end of the function name
|
|
|
|
(thus, for example, in NetBSD, FreeBSD, and glibc,
|
|
|
|
the equivalent of OpenBSDs "betoh32" is "be32toh").
|
endian.3: New page documenting byte order conversion functions
Document functions (new in glibc 2.9) for conversion between
host byte order and big-/little- endian byte order:
htobe16(), htole16(), be16toh(), le16toh(),
htobe32(), htole32(), be32toh(), le32toh(),
htobe64(), htole64(), be64toh(), le64toh()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-01-14 10:04:12 +00:00
|
|
|
.SH NOTES
|
|
|
|
These functions are similar to the older
|
|
|
|
.BR byteorder (3)
|
|
|
|
family of functions.
|
|
|
|
For example,
|
|
|
|
.BR be32toh ()
|
|
|
|
is identical to
|
|
|
|
.BR ntohl () .
|
|
|
|
|
|
|
|
The advantage of the
|
|
|
|
.BR byteorder (3)
|
|
|
|
functions is that they are standard functions available
|
intro.1, time.1, accept.2, bind.2, connect.2, execve.2, flock.2, getdents.2, getpriority.2, getuid.2, intro.2, ioctl.2, mincore.2, mknod.2, personality.2, ptrace.2, read.2, recv.2, select_tut.2, send.2, sendfile.2, shmctl.2, sigaction.2, signal.2, stat.2, times.2, truncate.2, umask.2, wait.2, MB_CUR_MAX.3, MB_LEN_MAX.3, argz_add.3, btowc.3, clearenv.3, clock.3, cmsg.3, end.3, endian.3, errno.3, exit.3, fgetwc.3, fgetws.3, fopen.3, fputwc.3, fputws.3, fseek.3, fwide.3, getfsent.3, getgrnam.3, gethostid.3, getipnodebyname.3, getmntent.3, getpwnam.3, getwchar.3, grantpt.3, iconv.3, iconv_close.3, iconv_open.3, insque.3, intro.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, malloc.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mkstemp.3, mktemp.3, nl_langinfo.3, openpty.3, posix_openpt.3, printf.3, ptsname.3, putwchar.3, qecvt.3, rcmd.3, readdir.3, rexec.3, rpc.3, setnetgrent.3, shm_open.3, sigpause.3, stdin.3, stpcpy.3, strftime.3, strptime.3, syslog.3, towctrans.3, towlower.3, towupper.3, ttyslot.3, ungetwc.3, unlocked_stdio.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcspbrk.3, wcsrchr.3, wcsrtombs.3, wcsspn.3, wcsstr.3, wcstok.3, wcstombs.3, wcswidth.3, wctob.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, wprintf.3, console_ioctl.4, pts.4, elf.5, filesystems.5, hosts.5, proc.5, ttytype.5, boot.7, capabilities.7, credentials.7, epoll.7, glob.7, koi8-r.7, path_resolution.7, pty.7, signal.7, suffixes.7, time.7, unicode.7, unix.7, uri.7, utf-8.7: global fix: s/Unix/UNIX/
The man pages were rather inconsistent in the use of "Unix"
versus "UNIX". Let's go with the trademark usage.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-10-12 04:45:38 +00:00
|
|
|
on all UNIX systems.
|
endian.3: New page documenting byte order conversion functions
Document functions (new in glibc 2.9) for conversion between
host byte order and big-/little- endian byte order:
htobe16(), htole16(), be16toh(), le16toh(),
htobe32(), htole32(), be32toh(), le32toh(),
htobe64(), htole64(), be64toh(), le64toh()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-01-14 10:04:12 +00:00
|
|
|
On the other hand, the fact that they were designed
|
|
|
|
for use in the context of TCP/IP means that
|
|
|
|
they lack the 64-bit and little-endian variants described in this page.
|
|
|
|
.SH EXAMPLE
|
|
|
|
The program below display the results of converting an integer
|
|
|
|
from host byte order to both little-endian and big-endian byte order.
|
|
|
|
Since host byte order is either little-endian or big-endian,
|
|
|
|
only one of these conversions will have an effect.
|
|
|
|
When we run this program on a little-endian system such as x86-32,
|
|
|
|
we see the following:
|
|
|
|
.in +4n
|
|
|
|
.nf
|
|
|
|
|
|
|
|
$ \fB./a.out\fP
|
|
|
|
x.u32 = 0x44332211
|
|
|
|
htole32(x.u32) = 0x44332211
|
|
|
|
htobe32(x.u32) = 0x11223344
|
|
|
|
.fi
|
|
|
|
.in
|
|
|
|
.SS Program source
|
|
|
|
\&
|
|
|
|
.nf
|
|
|
|
#include <endian.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
uint32_t u32;
|
|
|
|
uint8_t arr[4];
|
|
|
|
} x;
|
|
|
|
|
|
|
|
x.arr[0] = 0x11; /* Lowest-address byte */
|
|
|
|
x.arr[1] = 0x22;
|
|
|
|
x.arr[2] = 0x33;
|
|
|
|
x.arr[3] = 0x44; /* Highest-address byte */
|
|
|
|
|
|
|
|
printf("x.u32 = 0x%x\\n", x.u32);
|
|
|
|
printf("htole32(x.u32) = 0x%x\\n", htole32(x.u32));
|
|
|
|
printf("htobe32(x.u32) = 0x%x\\n", htobe32(x.u32));
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR byteorder (3)
|