.\" Copyright (C) 2009, Linux Foundation, written by Michael Kerrisk .\" .\" a few pieces remain from an earlier version .\" Copyright (C) 2008, Nanno Langstraat .\" .\" %%%LICENSE_START(VERBATIM) .\" 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. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2012 Akihiro MOTOKI .\" all rights reserved. .\" Translated 2012-05-05, Akihiro MOTOKI .\" .TH ENDIAN 3 2010\-09\-10 GNU "Linux Programmer's Manual" .SH 名前 htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh \- ホストバイトオーダーと ビッグ/リトルエンディアンバイトオーダーの間で値の変換を行う .SH 書式 .nf \fB#define _BSD_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP \fBuint16_t htobe16(uint16_t \fP\fIhost_16bits\fP\fB);\fP \fBuint16_t htole16(uint16_t \fP\fIhost_16bits\fP\fB);\fP \fBuint16_t be16toh(uint16_t \fP\fIbig_endian_16bits\fP\fB);\fP \fBuint16_t le16toh(uint16_t \fP\fIlittle_endian_16bits\fP\fB);\fP \fBuint32_t htobe32(uint32_t \fP\fIhost_32bits\fP\fB);\fP \fBuint32_t htole32(uint32_t \fP\fIhost_32bits\fP\fB);\fP \fBuint32_t be32toh(uint32_t \fP\fIbig_endian_32bits\fP\fB);\fP \fBuint32_t le32toh(uint32_t \fP\fIlittle_endian_32bits\fP\fB);\fP \fBuint64_t htobe64(uint64_t \fP\fIhost_64bits\fP\fB);\fP \fBuint64_t htole64(uint64_t \fP\fIhost_64bits\fP\fB);\fP \fBuint64_t be64toh(uint64_t \fP\fIbig_endian_64bits\fP\fB);\fP \fBuint64_t le64toh(uint64_t \fP\fIlittle_endian_64bits\fP\fB);\fP .fi .SH 説明 これらの関数は、整数値のバイトエンコーディングを、 使用中の CPU ("ホスト") のバイトオーダーから リトルエンディアンやビッグエンディアンバイトオーダーへの変換や その逆の変換を行う。 各関数の名前に付いている数字 \fInn\fP は、その関数が扱う整数のサイズ (16, 32, 64 ビットのどれか) を示している。 名前が "htobe\fInn\fP" という形の関数は、ホストバイトオーダーから ビッグエンディアンバイトオーダーへの変換を行う。 名前が "htole\fInn\fP" という形の関数は、ホストバイトオーダーから リトルエンディアンバイトオーダーへの変換を行う。 名前が "be\fInn\fPtoh" という形の関数は、ビッグエンディアンバイトオーダー からホストバイトオーダーへの変換を行う。 名前が "le\fInn\fPtoh" という形の関数は、リトルエンディアンバイトオーダー からホストバイトオーダーへの変換を行う。 .SH バージョン これらの関数は glibc バージョン 2.9 で追加された。 .SH 準拠 これらの関数は非標準である。 BSD には同様の関数が存在するが、 BSD では 必要なヘッダファイルは \fI\fP ではなく \fI\fP である。 不幸なことに、 NetBSD, FreeBSD, glibc では、 これらの関数の元々の OpenBSD での、 \fInn\fP は常に関数名の末尾に置く という名前付けルールが踏襲されていない (そのため、例を挙げると、 OpenBSD の "betoh32" と等価な関数は NetBSD, FreeBSD, glibc では "be32toh" となる)。 .SH 注意 これらの関数は、前からある \fBbyteorder\fP(3) 系の関数と同じである。 例えば、 \fBbe32toh\fP() は \fBntohl\fP() と等価である。 \fBbyteorder\fP(3) 系の関数のメリットは、これらが 全ての UNIX システムで利用可能な標準関数である点である。 一方で、これらの関数は TCP/IP 処理で使用されることを想定して 設計されたため、このページで説明している 64 ビット版や リトルエンディアン版などが存在しない。 .SH 例 以下のプログラムは、整数をホストバイトオーダーからリトルエンディアンと ビットエンディアンの両方のバイトオーダーに変換し、その結果を表示する。 ホストバイトオーダーはリトルエンディアンかビットエンディアンのいずれか なので、変換に意味があるのはどちらか一方だけである。 このプログラムを x86\-32 などのリトルエンディアンのシステムで実行した 場合の実行結果は下記のようになる。 .in +4n .nf $ \fB./a.out\fP x.u32 = 0x44332211 htole32(x.u32) = 0x44332211 htobe32(x.u32) = 0x11223344 .fi .in .SS プログラムのソース \& .nf #include #include #include #include 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\en", x.u32); printf("htole32(x.u32) = 0x%x\en", htole32(x.u32)); printf("htobe32(x.u32) = 0x%x\en", htobe32(x.u32)); exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBbyteorder\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。