ENDIAN(3) | Linux Programmer's Manual | ENDIAN(3) |
名前
htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh -ホストバイトオーダーとビッグ/リトルエンディアンバイトオーダーの間で値の変換を行う書式
#define _BSD_SOURCE /* feature_test_macros(7) 参照 */
#include <endian.h>
uint16_t htobe16(uint16_t host_16bits);
uint16_t htole16(uint16_t host_16bits);
uint16_t be16toh(uint16_t big_endian_16bits);
uint16_t le16toh(uint16_t little_endian_16bits);
uint32_t htobe32(uint32_t host_32bits);
uint32_t htole32(uint32_t host_32bits);
uint32_t be32toh(uint32_t big_endian_32bits);
uint32_t le32toh(uint32_t little_endian_32bits);
uint64_t htobe64(uint64_t host_64bits);
uint64_t htole64(uint64_t host_64bits);
uint64_t be64toh(uint64_t big_endian_64bits);
uint64_t le64toh(uint64_t little_endian_64bits);
説明
これらの関数は、整数値のバイトエンコーディングを、使用中の CPU ("ホスト") のバイトオーダーからリトルエンディアンやビッグエンディアンバイトオーダーへの変換やその逆の変換を行う。バージョン
これらの関数は glibc バージョン 2.9 で追加された。準拠
これらの関数は非標準である。 BSD には同様の関数が存在するが、 BSD では必要なヘッダファイルは <endian.h> ではなく <sys/endian.h> である。不幸なことに、 NetBSD, FreeBSD, glibc では、これらの関数の元々の OpenBSD での、 nn は常に関数名の末尾に置くという名前付けルールが踏襲されていない (そのため、例を挙げると、 OpenBSD の "betoh32"と等価な関数は NetBSD, FreeBSD, glibc では "be32toh"となる)。注意
これらの関数は、前からある byteorder(3) 系の関数と同じである。例えば、 be32toh() は ntohl() と等価である。例
以下のプログラムは、整数をホストバイトオーダーからリトルエンディアンとビットエンディアンの両方のバイトオーダーに変換し、その結果を表示する。ホストバイトオーダーはリトルエンディアンかビットエンディアンのいずれかなので、変換に意味があるのはどちらか一方だけである。このプログラムを x86-32 などのリトルエンディアンのシステムで実行した場合の実行結果は下記のようになる。
$ ./a.out
x.u32 = 0x44332211
htole32(x.u32) = 0x44332211
htobe32(x.u32) = 0x11223344
プログラムのソース
#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);
}
関連項目
byteorder(3)この文書について
この man ページは Linux man-pages プロジェクトのリリース 3.51 の一部である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man-pages/ に書かれている。2010-09-10 | GNU |