Lit*_*ony 3 c format printf hex
想要打印 uint32 十六进制。尝试过“PRIx32”,对我来说看起来很糟糕:
967407c0 23 4481d55f ffffff52 de3cd140
2fc aa7363cf fffff40d 563270c0 2b86
Run Code Online (Sandbox Code Playgroud)
所以...尝试了这个。肮脏但有效:
/*******************************************************************************
Print a digit in hex.
*******************************************************************************/
void mpal_digit_t_print_hex(
mpal_digit_t * input)
{
uint8_t p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8;
p_1 = (uint8_t)(((*input) << 0) >> 28);
p_2 = (uint8_t)(((*input) << 4) >> 28);
p_3 = (uint8_t)(((*input) << 8) >> 28);
p_4 = (uint8_t)(((*input) << 12) >> 28);
p_5 = (uint8_t)(((*input) << 16) >> 28);
p_6 = (uint8_t)(((*input) << 20) >> 28);
p_7 = (uint8_t)(((*input) << 24) >> 28);
p_8 = (uint8_t)(((*input) << 28) >> 28);
printf(" %" PRIx8 "%" PRIx8 "%" PRIx8 "%" PRIx8 "%" PRIx8 "%" PRIx8 "%"
PRIx8 "%" PRIx8 "", p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8);
return;
}
Run Code Online (Sandbox Code Playgroud)
输出:
61dd90ef ff6df47e a0a9abc0 011d7394 2311761f
f4402427 5c44ca40 0f86aba7 6aa5194f ecb9ed1e
Run Code Online (Sandbox Code Playgroud)
在c中有没有一种优雅的方法来做到这一点?
这些“符号” printf 说明符旨在PRIx32与额外的标志一起使用(如果您需要的话)。所以你可以这样做:
printf("%08" PRIx32, hexval);
Run Code Online (Sandbox Code Playgroud)
字符串连接后,通常会变成%08x或%08lx。 8指定您需要(至少)8 位数字,并0指定 0 填充。