{"id":6886,"date":"2022-12-20T19:34:15","date_gmt":"2022-12-20T22:34:15","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/endian-man3\/"},"modified":"2022-12-20T19:34:15","modified_gmt":"2022-12-20T22:34:15","slug":"endian-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/endian-man3\/","title":{"rendered":"ENDIAN (man3)"},"content":{"rendered":"<h1 align=\"center\">ENDIAN<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#VERSIONS\">VERSIONS<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh \u2212 convert values between host and big-\/little-endian byte order<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <endian.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>uint16_t htobe16(uint16_t<\/b> <i>host_16bits<\/i><b>); <br \/> uint16_t htole16(uint16_t<\/b> <i>host_16bits<\/i><b>); <br \/> uint16_t be16toh(uint16_t<\/b> <i>big_endian_16bits<\/i><b>); <br \/> uint16_t le16toh(uint16_t<\/b> <i>little_endian_16bits<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>uint32_t htobe32(uint32_t<\/b> <i>host_32bits<\/i><b>); <br \/> uint32_t htole32(uint32_t<\/b> <i>host_32bits<\/i><b>); <br \/> uint32_t be32toh(uint32_t<\/b> <i>big_endian_32bits<\/i><b>); <br \/> uint32_t le32toh(uint32_t<\/b> <i>little_endian_32bits<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>uint64_t htobe64(uint64_t<\/b> <i>host_64bits<\/i><b>); <br \/> uint64_t htole64(uint64_t<\/b> <i>host_64bits<\/i><b>); <br \/> uint64_t be64toh(uint64_t<\/b> <i>big_endian_64bits<\/i><b>); <br \/> uint64_t le64toh(uint64_t<\/b> <i>little_endian_64bits<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:5%; margin-top: 1em\">Feature Test Macro Requirements for glibc (see <b>feature_test_macros<\/b>(7)):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>htobe16<\/b>(), <b>htole16<\/b>(), <b>be16toh<\/b>(), <b>le16toh<\/b>(), <b>htobe32<\/b>(), <b>htole32<\/b>(), <b>be32toh<\/b>(), <b>le32toh<\/b>(), <b>htobe64<\/b>(), <b>htole64<\/b>(), <b>be64toh<\/b>(), <b>le64toh<\/b>(): <br \/> Since glibc 2.19: <br \/> _DEFAULT_SOURCE <br \/> In glibc up to and including 2.19: <br \/> _BSD_SOURCE<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions convert the byte encoding of integer values from the byte order that the current CPU (the &#8220;host&#8221;) uses, to and from little-endian and big-endian byte order.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The number, <i>nn<\/i>, in the name of each function indicates the size of integer handled by the function, either 16, 32, or 64 bits.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The functions with names of the form &#8220;htobe<i>nn<\/i>&#8221; convert from host byte order to big-endian order.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The functions with names of the form &#8220;htole<i>nn<\/i>&#8221; convert from host byte order to little-endian order.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The functions with names of the form &#8220;be<i>nn<\/i>toh&#8221; convert from big-endian order to host byte order.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The functions with names of the form &#8220;le<i>nn<\/i>toh&#8221; convert from little-endian order to host byte order.<\/p>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions were added to glibc in version 2.9.<\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions are nonstandard. Similar functions are present on the BSDs, where the required header file is <i><sys\/endian.h><\/i> instead of <i><endian.h><\/i>. Unfortunately, NetBSD, FreeBSD, and glibc haven\u2019t followed the original OpenBSD naming convention for these functions, whereby the <i>nn<\/i> component always appears at the end of the function name (thus, for example, in NetBSD, FreeBSD, and glibc, the equivalent of OpenBSDs &#8220;betoh32&#8221; is &#8220;be32toh&#8221;).<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions are similar to the older <b>byteorder<\/b>(3) family of functions. For example, <b>be32toh<\/b>() is identical to <b>ntohl<\/b>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The advantage of the <b>byteorder<\/b>(3) functions is that they are standard functions available on all UNIX systems. 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.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>.\/a.out<\/b> <br \/> x.u32 = 0x44332211 <br \/> htole32(x.u32) = 0x44332211 <br \/> htobe32(x.u32) = 0x11223344<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Program source<\/b> <br \/> #include <endian.h> <br \/> #include <stdint.h> <br \/> #include <stdio.h> <br \/> #include <stdlib.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> union { <br \/> uint32_t u32; <br \/> uint8_t arr[4]; <br \/> } x;<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<p>x.arr[0] = 0x11;<\/p>\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"30%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"54%\">\n<p>\/* Lowest-address byte *\/<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\">x.arr[1] = 0x22; <br \/> x.arr[2] = 0x33;<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<p>x.arr[3] = 0x44;<\/p>\n<td width=\"17%\"><\/td>\n<td width=\"-9%\"><\/td>\n<td width=\"30%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"54%\">\n<p>\/* Highest-address byte *\/<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;x.u32 = %#xn&#8221;, x.u32); <br \/> printf(&#8220;htole32(x.u32) = %#xn&#8221;, htole32(x.u32)); <br \/> printf(&#8220;htobe32(x.u32) = %#xn&#8221;, htobe32(x.u32));<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>bswap<\/b>(3), <b>byteorder<\/b>(3)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh \u2212 convert values between host and big-\/little-endian byte order <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,3248,3007],"class_list":["post-6886","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-endian","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6886"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6886\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}