OSDN Git Service

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