3 回答

TA贡献1966条经验 获得超4个赞
文档:man htobe64在Linux(glibc> = 2.9)或FreeBSD上。
不幸的是,在2009年,OpenBSD,FreeBSD和glibc(Linux)并没有很好地协同工作来为此创建一个(非内核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库”式方法。
- 3 回答
- 0 关注
- 1799 浏览
添加回答
举报