endian

Section: C Library Functions (3)
Updated: 2025-05-17
Index JM Home Page roff page
 

名前

htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh - ホストバイトオーダーと ビッグ/リトルエンディアンバイトオーダーの間で値の変換を行う  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#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);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

htobe16(), htole16(), be16toh(), le16toh(), htobe32(), htole32(), be32toh(), le32toh(), htobe64(), htole64(), be64toh(), le64toh():

    glibc 2.19 以降:
        _DEFAULT_SOURCE
    glibc 2.19 以前:
        _BSD_SOURCE
 

説明

これらの関数は、整数値のバイトエンコーディングを、 使用中の CPU ("ホスト") のバイトオーダーから リトルエンディアンやビッグエンディアンバイトオーダーへの変換や その逆の変換を行う。

各関数の名前に付いている数字 nn は、その関数が扱う整数のサイズ (16, 32, 64 ビットのどれか) を示している。

The functions with names of the form "htobenn" convert from host byte order to big-endian order.

The functions with names of the form "htolenn" convert from host byte order to little-endian order.

The functions with names of the form "benntoh" convert from big-endian order to host byte order.

The functions with names of the form "lenntoh" convert from little-endian order to host byte order.  

バージョン

Similar functions are present on the BSDs, where the required header file is <sys/endian.h> instead of <endian.h>. Unfortunately, NetBSD, FreeBSD, and glibc haven't followed the original OpenBSD naming convention for these functions, whereby the nn component always appears at the end of the function name (thus, for example, in NetBSD, FreeBSD, and glibc, the equivalent of OpenBSDs "betoh32" is "be32toh").  

標準規格

なし。  

歴史

glibc 2.9.

これらの関数は、前からある byteorder(3) 系の関数と同じである。 例えば、 be32toh() は ntohl() と等価である。

byteorder(3) 系の関数のメリットは、これらが 全ての UNIX システムで利用可能な標準関数である点である。 一方で、これらの関数は TCP/IP 処理で使用されることを想定して 設計されたため、このページで説明している 64 ビット版や リトルエンディアン版などが存在しない。  

例

以下のプログラムは、整数をホストバイトオーダーからリトルエンディアンと ビットエンディアンの両方のバイトオーダーに変換し、その結果を表示する。 ホストバイトオーダーはリトルエンディアンかビットエンディアンのいずれか なので、変換に意味があるのはどちらか一方だけである。 このプログラムを 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(void)
{
    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 = %#x\n", x.u32);
    printf("htole32(x.u32) = %#x\n", htole32(x.u32));
    printf("htobe32(x.u32) = %#x\n", htobe32(x.u32));

    exit(EXIT_SUCCESS);
}
 

関連項目

bswap(3), byteorder(3)


 

Index

名前
ライブラリ
書式
説明
バージョン
標準規格
歴史
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.