我正在使用C和bswap_ {16,32,64}来自byteswap.h的宏从big-endian读取/编写一个小端格式的二进制文件,用于字节交换.
除了40位的位字段外,所有值都被正确读取和写入.
该bswap_40宏不存在,我不知道该怎么做,或有没有更好的解决方案是可能的.
这是一个显示此问题的小代码:
#include <stdio.h>
#include <inttypes.h>
#include <byteswap.h>
#define bswap_40(x) bswap_64(x)
struct tIndex {
uint64_t val_64;
uint64_t val_40:40;
} s1 = { 5294967296, 5294967296 };
int main(void)
{
// write swapped values
struct tIndex s2 = { bswap_64(s1.val_64), bswap_40(s1.val_40) };
FILE *fp = fopen("index.bin", "w");
fwrite(&s2, sizeof(s2), 1, fp);
fclose(fp);
// read swapped values
struct tIndex s3;
fp = fopen("index.bin", "r");
fread(&s3, sizeof(s3), 1, fp);
fclose(fp);
s3.val_64 = bswap_64(s3.val_64);
s3.val_40 = …Run Code Online (Sandbox Code Playgroud)