3 回答

TA貢獻1966條經驗 獲得超4個贊
文檔:man htobe64在Linux(glibc> = 2.9)或FreeBSD上。
不幸的是,在2009年,OpenBSD,F(xiàn)reeBSD和glibc(Linux)并沒有很好地協(xié)同工作來為此創(chuàng)建一個(非內核API)libc標準。
當前,這段簡短的預處理器代碼:
#if defined(__linux__)
# include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__)
# include <sys/endian.h>
#elif defined(__OpenBSD__)
# include <sys/types.h>
# define be16toh(x) betoh16(x)
# define be32toh(x) betoh32(x)
# define be64toh(x) betoh64(x)
#endif
(在Linux和OpenBSD上測試)應該隱藏差異。它為您提供了這4個平臺上的Linux / FreeBSD風格的宏。
使用示例:
#include <stdint.h> // For 'uint64_t'
uint64_t host_int = 123;
uint64_t big_endian;
big_endian = htobe64( host_int );
host_int = be64toh( big_endian );
這是目前最“標準的C庫”式方法。
添加回答
舉報