将数据转换回其原始项目

Converting data back to its original items

我有一些代码,但我不明白如何取回个别项目:

u32_t foo = ((u32_t)((d) & 0xff) << 24) | ((u32_t)((c) & 0xff) << 16) |  
((u32_t)((b) & 0xff) << 8)  | (u32_t)((a) & 0xff)

我要输入这个转换的结果 我希望将 IP 地址转换回其部分以显示。 但是当我输入 192 168 1 200 时,我得到 0xC801A8C0 并且我没有将它转换回来。

有谁知道如何在联合结构中添加项? 我尝试使用 LwIP 但联合结构有问题。我尝试访问 local_ip 和 remote_ip。

u32_t d = (foo >> 24) & 0xFF; // Get the bits 32nd...25th
u32_t c = (foo >> 16) & 0xFF; // 24th...17th
u32_t b = (foo >> 8) & 0xFF;  // 16th...9th
u32_t a = (foo) & 0xFF;       // 8th...1st

这实际上与上面的代码相反。

你可以屏蔽掉里面的值 foo

u32_t thisWasD = (foo & 0xff000000) >> 24; // ==> will null out the lowest 24 bits
u32_t thisWasC = (foo & 0x00ff0000) >> 16; // ==> will null out the upper 8 and lowest 16 bit
u32_t thisWasB = (foo & 0x0000ff00) >> 8; // ==> etc
u32_t thisWasA = (foo & 0xff) // etc - no shift needed

然后将它们移回,使它们在拳头 8 位中对齐(按 24、16、8 - 最后一个已经可以了

这就像你有一个 int input 并且你想逐字节划分并将每个字节存储在单独的整数中

int main() {


         a = ((uint32_t)((foo) & 0x000000ff) << 24);
         b = ((uint32_t)((foo) & 0x0000ff00) << 16);
         c = ((uint32_t)((foo) & 0x00ff0000) << 8);
         d = ((uint32_t)((foo) & 0xff000000);

}