OSDN Git Service

(split) LDP: Add new releases for LDP v3.40.
[linuxjm/LDP_man-pages.git] / release / man3 / endian.3
1 .\" Copyright (C) 2009, Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\" a few pieces remain from an earlier version
4 .\" Copyright (C) 2008, Nanno Langstraat <nal@ii.nl>
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\"*******************************************************************
27 .\"
28 .\" This file was generated with po4a. Translate the source file.
29 .\"
30 .\"*******************************************************************
31 .TH ENDIAN 3 2010\-09\-10 GNU "Linux Programmer's Manual"
32 .SH 名前
33 htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh,
34 le32toh, htobe64, htole64, be64toh, le64toh \- ホストバイトオーダーと
35 ビッグ/リトルエンディアンバイトオーダーの間で値の変換を行う
36 .SH 書式
37 .nf
38 \fB#define _BSD_SOURCE\fP             /* feature_test_macros(7) 参照 */
39 \fB#include <endian.h>\fP
40
41 \fBuint16_t htobe16(uint16_t \fP\fIhost_16bits\fP\fB);\fP
42 \fBuint16_t htole16(uint16_t \fP\fIhost_16bits\fP\fB);\fP
43 \fBuint16_t be16toh(uint16_t \fP\fIbig_endian_16bits\fP\fB);\fP
44 \fBuint16_t le16toh(uint16_t \fP\fIlittle_endian_16bits\fP\fB);\fP
45
46 \fBuint32_t htobe32(uint32_t \fP\fIhost_32bits\fP\fB);\fP
47 \fBuint32_t htole32(uint32_t \fP\fIhost_32bits\fP\fB);\fP
48 \fBuint32_t be32toh(uint32_t \fP\fIbig_endian_32bits\fP\fB);\fP
49 \fBuint32_t le32toh(uint32_t \fP\fIlittle_endian_32bits\fP\fB);\fP
50
51 \fBuint64_t htobe64(uint64_t \fP\fIhost_64bits\fP\fB);\fP
52 \fBuint64_t htole64(uint64_t \fP\fIhost_64bits\fP\fB);\fP
53 \fBuint64_t be64toh(uint64_t \fP\fIbig_endian_64bits\fP\fB);\fP
54 \fBuint64_t le64toh(uint64_t \fP\fIlittle_endian_64bits\fP\fB);\fP
55 .fi
56 .SH 説明
57 これらの関数は、整数値のバイトエンコーディングを、
58 使用中の CPU ("ホスト") のバイトオーダーから
59 リトルエンディアンやビッグエンディアンバイトオーダーへの変換や
60 その逆の変換を行う。
61
62 各関数の名前に付いている数字 \fInn\fP は、その関数が扱う整数のサイズ
63 (16, 32, 64 ビットのどれか) を示している。
64
65 名前が "htobe\fInn\fP" という形の関数は、ホストバイトオーダーから
66 ビッグエンディアンバイトオーダーへの変換を行う。
67
68 名前が "htole\fInn\fP" という形の関数は、ホストバイトオーダーから
69 リトルエンディアンバイトオーダーへの変換を行う。
70
71 名前が "be\fInn\fPtoh" という形の関数は、ビッグエンディアンバイトオーダー
72 からホストバイトオーダーへの変換を行う。
73
74 名前が "le\fInn\fPtoh" という形の関数は、リトルエンディアンバイトオーダー
75 からホストバイトオーダーへの変換を行う。
76 .SH バージョン
77 これらの関数は glibc バージョン 2.9 で追加された。
78 .SH 準拠
79 これらの関数は非標準である。
80 BSD には同様の関数が存在するが、 BSD では
81 必要なヘッダファイルは \fI<endian.h>\fP ではなく
82 \fI<sys/endian.h>\fP である。
83 不幸なことに、 NetBSD, FreeBSD, glibc では、
84 これらの関数の元々の OpenBSD での、 \fInn\fP は常に関数名の末尾に置く
85 という名前付けルールが踏襲されていない
86 (そのため、例を挙げると、 OpenBSD の "betoh32" と等価な関数は
87 NetBSD, FreeBSD, glibc では "be32toh" となる)。
88 .SH 注意
89 これらの関数は、前からある \fBbyteorder\fP(3) 系の関数と同じである。
90 例えば、 \fBbe32toh\fP() は \fBntohl\fP() と等価である。
91
92 \fBbyteorder\fP(3) 系の関数のメリットは、これらが
93 全ての UNIX システムで利用可能な標準関数である点である。
94 一方で、これらの関数は TCP/IP 処理で使用されることを想定して
95 設計されたため、このページで説明している 64 ビット版や
96 リトルエンディアン版などが存在しない。
97 .SH 例
98 以下のプログラムは、整数をホストバイトオーダーからリトルエンディアンと
99 ビットエンディアンの両方のバイトオーダーに変換し、その結果を表示する。
100 ホストバイトオーダーはリトルエンディアンかビットエンディアンのいずれか
101 なので、変換に意味があるのはどちらか一方だけである。
102 このプログラムを x86\-32 などのリトルエンディアンのシステムで実行した
103 場合の実行結果は下記のようになる。
104 .in +4n
105 .nf
106
107 $ \fB./a.out\fP
108 x.u32 = 0x44332211
109 htole32(x.u32) = 0x44332211
110 htobe32(x.u32) = 0x11223344
111 .fi
112 .in
113 .SS プログラムのソース
114 \&
115 .nf
116 #include <endian.h>
117 #include <stdint.h>
118 #include <stdio.h>
119 #include <stdlib.h>
120
121 int
122 main(int argc, char *argv[])
123 {
124     union {
125         uint32_t u32;
126         uint8_t arr[4];
127     } x;
128
129     x.arr[0] = 0x11;    /* Lowest\-address byte */
130     x.arr[1] = 0x22;
131     x.arr[2] = 0x33;
132     x.arr[3] = 0x44;    /* Highest\-address byte */
133
134     printf("x.u32 = 0x%x\en", x.u32);
135     printf("htole32(x.u32) = 0x%x\en", htole32(x.u32));
136     printf("htobe32(x.u32) = 0x%x\en", htobe32(x.u32));
137
138     exit(EXIT_SUCCESS);
139 }
140 .fi
141 .SH 関連項目
142 \fBbyteorder\fP(3)
143 .SH この文書について
144 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
145 である。プロジェクトの説明とバグ報告に関する情報は
146 http://www.kernel.org/doc/man\-pages/ に書かれている。