如何将u8数组转换为u32?

时间:2016-10-01 08:32:42

标签: c arrays int hex

如何将u8数组转换为u32?

例如:

u8 a[4];
u32 b; 

a[0] = 0x00;
a[1] = 0x00;
a[2] = 0x00;
a[3] = 0x09;

如何将数组a(8bitx4 = 32bit)转换为b(32bit)?

1 个答案:

答案 0 :(得分:-1)

unsigned char a[4];
unsigned int b;

a[0] = 0x00;
a[1] = 0x00;
a[2] = 0x00;
a[3] = 0x09;

int n = 1;
if (*(char *)&n == 1) {
    std::reverse(std::begin(a), std::end(a));
}

b = *reinterpret_cast<unsigned int *>(&a);