C言語でのエンディアンの変換
#TODO
C言語でエンディアン変換をする場合、endian.hとかnetinet/in.hを使う。
ホストがリトルエンディアンで、endian.hを使う場合、
ビッグ→リトルならbe16tohとかbe32toh
リトル→ビッグならhtobe16とかhtobe32
code:c
#include <stdio.h>
#include <endian.h>
ソケット通信などのデータを送受信する系の場合
code:c
#include <netinet/in.h>
// or
#include <arpa/inet.h>
code:c
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
参考
Ubuntu Manpage: htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64,
Ubuntu Manpage: htonl, htons, ntohl, ntohs - ホストバイトオーダーとネットワークバイトオーダーの間で値を変換する
関連
ソケット通信(C)