我得到了一个来自 gzread 的调用的 cstring。我知道数据是块,每个块由一个无符号整数、字符、整数和无符号短整数组成。
所以我想知道将这个 cstring 拆分为适当变量的标准方法是什么。
说前 4 个字节,是 unsigned int,下一个字节是 char,接下来 4 个字节是有符号 int,最后 2 个字节是 unsigned short int。
//Some pseudocode below which would work
char buf[11];
unsigned int a;
char b;
int c;
unsigned short int d;
我想我可以用适当的偏移量来记忆。
memcpy(&a, buf, sizeof(unsigned int));
memcpy(&b, buf+4, sizeof(char));
memcpy(&c, buf+5, sizeof(int));
memcpy(&d, buf+9, sizeof(unsigned short int));
还是使用一些位运算符更好?比如移位和掩蔽。
或者将所有 11 个字节直接 gzreading 到某个结构中会更好,或者这甚至可能吗?结构的内存布局是否已修复,这是否适用于 gzread?