htobe64 和 endian.h 的可移植性
portability of htobe64 and endian.h
我需要将我在运行我的代码的任意机器上收到的 8 个字节按大端顺序排列。我相信我可以为此使用 htobe64
函数,但我不确定它的可移植性 - 即 endian.h
的可用性 - 在编译我的代码时跨不同的机器架构和操作系统。这是一种安全的可移植方法,还是使用其他方法更好?
请使用以下可移植方法:
#include <stdint.h>
void write64be(unsigned char out[8], uint64_t in)
{
out[0] = in >> 56 & 0xff;
out[1] = in >> 48 & 0xff;
out[2] = in >> 40 & 0xff;
out[3] = in >> 32 & 0xff;
out[4] = in >> 24 & 0xff;
out[5] = in >> 16 & 0xff;
out[6] = in >> 8 & 0xff;
out[7] = in >> 0 & 0xff;
}
我需要将我在运行我的代码的任意机器上收到的 8 个字节按大端顺序排列。我相信我可以为此使用 htobe64
函数,但我不确定它的可移植性 - 即 endian.h
的可用性 - 在编译我的代码时跨不同的机器架构和操作系统。这是一种安全的可移植方法,还是使用其他方法更好?
请使用以下可移植方法:
#include <stdint.h>
void write64be(unsigned char out[8], uint64_t in)
{
out[0] = in >> 56 & 0xff;
out[1] = in >> 48 & 0xff;
out[2] = in >> 40 & 0xff;
out[3] = in >> 32 & 0xff;
out[4] = in >> 24 & 0xff;
out[5] = in >> 16 & 0xff;
out[6] = in >> 8 & 0xff;
out[7] = in >> 0 & 0xff;
}