order
参数指示是否要从最高有效字节开始。
void printAsChars(uint32_t val, int order)
{
if(!order)
{
putchar((val >> 0) & 0xff);
putchar((val >> 8) & 0xff);
putchar((val >> 16) & 0xff);
putchar((val >> 24) & 0xff);
}
else
{
putchar((val >> 24) & 0xff);
putchar((val >> 16) & 0xff);
putchar((val >> 8) & 0xff);
putchar((val >> 0) & 0xff);
}
}
int main(int argc, char* argv[])
{
printAsChars(0x44585433,0); putchar('\n');
printAsChars(0x44585433,1); putchar('\n');
}
https://godbolt.org/z/WWE9Yr
另外的选择
int main(int argc, char* argv[])
{
uint32_t val = 0x44585433;
printf("%.4s", (char *)&val);
}
https://godbolt.org/z/eEj755