gpt4 book ai didi

c - 将字节数组打印为格式化文本会给出不同的输出 - C

转载 作者:太空宇宙 更新时间:2023-11-04 02:50:10 25 4
gpt4 key购买 nike

我试图在 for 循环中以十六进制格式一次将字节数组打印为一个字节,如下所示:

int my_function(void *data)
{
obuf = (str*)data;

int i;
for (i = 0; i < obuf->len; i++)
{
printf("%02X:", obuf->s[i]);
}

return 0;
}

str 在这种情况下是来自 Kamailio 的结构 - 查看 http://www.asipto.com/pub/kamailio-devel-guide/#c05str

预期输出:

80:70:0F:80:00:00:96:00:1D:54:7D:7C:36:9D:1B:9A:20:BF:F9:68:E8:E8:E8:F8:68:98:E8:EE:E8:B4:7C:3C:34:74:74:64:74:69:2C:5A:3A:3A:3A:3A:3A:3A:32:24:43:AD:19:1D:1D:1D:1D:13:1D:1B:3B:60:AB:AB:AB:AB:AB:0A:BA:BA:BA:BA:B0:AB:AB:AB:AB:AB:0A:BA:BA:BA:BA:B9:3B:61:88:43:

我得到了什么:

FFFFFF80:70:0F:FFFFFF80:00:00:FFFFFF96:00:1D:54:7D:7C:36:FFFFFF9D:1B:FFFFFF9A:20:FFFFFFBF:FFFFFFF9:68:FFFFFFE8:FFFFFFE8:FFFFFFE8:FFFFFFF8:68:FFFFFF98:FFFFFFE8:FFFFFFEE:FFFFFFE8:FFFFFFB4:7C:3C:34:74:74:64:74:69:2C:5A:3A:3A:3A:3A:3A:3A:32:24:43:FFFFFFAD:19:1D:1D:1D:1D:13:1D:1B:3B:60:FFFFFFAB:FFFFFFAB:FFFFFFAB:FFFFFFAB:FFFFFFAB:0A:FFFFFFBA:FFFFFFBA:FFFFFFBA:FFFFFFBA:FFFFFFB0:FFFFFFAB:FFFFFFAB:FFFFFFAB:FFFFFFAB:FFFFFFAB:0A:FFFFFFBA:FFFFFFBA:FFFFFFBA:FFFFFFBA:FFFFFFB9:3B:61:FFFFFF88:43:

谁能帮我理解为什么有些字节以 FFFFFF 为前缀而其他字节没有?

提前致谢

最佳答案

看起来 obuf->s[i] 返回一个带符号的值

您需要将其转换为无符号值以在开始时摆脱 FFF..。

printf("%02X:", (unsigned char)(obuf->s[i]));

关于c - 将字节数组打印为格式化文本会给出不同的输出 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23010061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com