[英]byte array to struct - how to access struct members
我将以下字节数组转换为以下结构:**我知道这里的格式不是正确的"0x80",
等,但是在我的代码中是这样。
unsigned char ReadBuffer[512] = { 80 00 00 00 50 00 00 00 01 00 40 00 00 00 01 00 00 00 00 00 00 00 00 00 FF F2 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 30 0F 00 00 00 00 00 00 30 0F 00 00 00 00 00 00 30 0F 00 00 00 00 33 20 C8 00 00 00 0C 42 E0 2A 0F 9F B9 00 00 FF}
typedef struct MFT_ATTRIBUTE {
DWORD dwType;
DWORD dwFullLength;
BYTE uchNonResFlag;
BYTE uchNameLength;
WORD wNameOffset;
WORD wFlags;
WORD wID;
LONG n64StartVCN;
LONG n64EndVCN;
WORD wDatarunOffset;
WORD wCompressionSize;
BYTE uchPadding[4];
LONGLONG n64AllocSize;
LONGLONG n64RealSize;
LONGLONG n64StreamSize;
} MFT_ATTRIBUTE, *P_MFT_ATTRIBUTE;
MFT_ATTRIBUTE* mft_attribute = (MFT_ATTRIBUTE*)ReadBuffer[0];
当我尝试打印成员时,由于某种原因,我得到了某种增量值:
printf("%x ",&mft_attribute->dwType);
printf("%x ",&mft_attribute->dwFullLength);
printf("%x ",&mft_attribute->uchNonResFlag);
printf("%x ",&mft_attribute->uchNameLength);
Output:
0x80 0x84 0x88 0x89
有人可以帮我澄清一下吗?
您正在打印地址,而不是值。 这就是为什么输出以这种方式增加的原因:
dwType
基地址,与第一个成员dwType
相同 在输出代码中删除&在mft_attribute之前:
printf("%x ", mft_attribute->dwType);
printf("%x ", mft_attribute->dwFullLength);
printf("%x ", mft_attribute->uchNonResFlag);
printf("%x ", mft_attribute->uchNameLength);
您正在将数组的第一个元素转换为指向您的结构的指针。
MFT_ATTRIBUTE* mft_attribute = (MFT_ATTRIBUTE*)ReadBuffer[0];
您想要将指针投射到第一个元素:
MFT_ATTRIBUTE* mft_attribute = (MFT_ATTRIBUTE*) (ReadBuffer + 0);
就像@Wolf指出的那样,这会打印指针而不是指向的值:
printf("%x ",&mft_attribute->dwType);
您需要
printf("%x ", mft_attribute->dwType);
将演员表更改为以下内容:
MFT_ATTRIBUTE* mft_attribute = (MFT_ATTRIBUTE*)&ReadBuffer[0];
或者
MFT_ATTRIBUTE* mft_attribute = (MFT_ATTRIBUTE*)ReadBuffer;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.