以十六进制和十进制打印 unsigned short int C
Print unsigned short int in hex and decimal C
我一直试图在 C 中打印 unsigned short int
值,但没有成功。据我所知,它是一个 16 位值,所以我尝试了几种不同的方法来一起打印这 2 个字节,但我只能在逐字节打印时才能正确打印它。
请注意,我想以十进制和十六进制格式打印这 16 位,例如 00 01
as 1
,另一个示例是:00 ff
as 255
.
我有这个结构:
struct arphdr {
unsigned short int ar_hrd;
unsigned short int ar_pro;
unsigned char ar_hln;
unsigned char ar_pln;
unsigned short int ar_op;
// I'm commenting the following part because I won't need it now
// for the explanation
/* unsigned char __ar_sha[ETH_ALEN];
unsigned char __ar_sip[4];
unsigned char __ar_tha[ETH_ALEN];
unsigned char __ar_tip[4]; */
};
还有这个函数:
void print_ARP_msg(struct arphdr arp_hdr) {
// I need to print the arp_hdr.ar_hrd in both decimal and hex.
printf("Format HW: %04x\n", arp_hdr.ar_hrd);
printf("Format Proto: %04x\n", arp_hdr.ar_pro);
printf("HW Len: %x\n", arp_hdr.ar_hln);
printf("Proto Len: %x\n", arp_hdr.ar_pln);
printf("Command: %04x\n", arp_hdr.ar_op);
}
print_ARP_msg函数returns我这个:
Format HW: 0100
Format Proto: 0008
HW Len: 6
Proto Len: 4
Command: 0100
该结构的十六进制值为“00 01 08 00 06 04 00 01”,所以我不知道为什么它在 arp_hdr.ar_hrd 值中返回 0100。
另外,我制作了一个以十六进制打印结构的函数,以确保我做的是正确的,并且我能够检查所有字段是否正确分配。
PS:在发这个问题之前,我发现它打印的是正确的十六进制值,但是乱七八糟。可能与 little/big 字节序“差异”有关吗?
The hex values of the struct are "00 01 08 00 06 04 00 01", so I don't know why it's returning me 0100 in the arp_hdr.ar_hrd value.
看起来你的平台使用小端系统。
在记忆中似乎是 00 01
的内容被解释为 01 x 2^8 + 00
。换句话说,数字的十六进制表示是 0100
.
Could it be related to the little/big endian "difference"?
是的。如果您正在处理通过网络到达的数据包 - 并且您正在打印 ARP 数据包的字段,那么这正是您正在做的 - 您可能必须从字段的字节顺序转换为通过网络发送到您 运行.
机器上的字节顺序
例如:
printf("Format HW: %04x\n", ntohs(arp_hdr.ar_hrd));
在这种特殊情况下,您可以在大端计算机(SPARC、System/3x0、z/Architecture、PowerPC/Power ISA 运行 AIX, PowerPC/Power ISA 运行 Mac OS X, 可能 PowerPC/Power ISA 运行 Linux, 等等),但是在小端机器(任何带有 x86 处理器的机器,包括 x86-64 处理器,可能是大多数 ARM 等)上,如果没有它,你就无法逃脱。
您可以在两种类型的处理器上使用它。
ARP 数据包与所有互联网协议数据包一样,以 "network order" 传输,即大端。例如,客户端必须使用 ntohs
(网络到主机的简称)将网络顺序转换为机器的本地字节顺序,而 htons
则相反。
详情见man byteorder
。
我一直试图在 C 中打印 unsigned short int
值,但没有成功。据我所知,它是一个 16 位值,所以我尝试了几种不同的方法来一起打印这 2 个字节,但我只能在逐字节打印时才能正确打印它。
请注意,我想以十进制和十六进制格式打印这 16 位,例如 00 01
as 1
,另一个示例是:00 ff
as 255
.
我有这个结构:
struct arphdr {
unsigned short int ar_hrd;
unsigned short int ar_pro;
unsigned char ar_hln;
unsigned char ar_pln;
unsigned short int ar_op;
// I'm commenting the following part because I won't need it now
// for the explanation
/* unsigned char __ar_sha[ETH_ALEN];
unsigned char __ar_sip[4];
unsigned char __ar_tha[ETH_ALEN];
unsigned char __ar_tip[4]; */
};
还有这个函数:
void print_ARP_msg(struct arphdr arp_hdr) {
// I need to print the arp_hdr.ar_hrd in both decimal and hex.
printf("Format HW: %04x\n", arp_hdr.ar_hrd);
printf("Format Proto: %04x\n", arp_hdr.ar_pro);
printf("HW Len: %x\n", arp_hdr.ar_hln);
printf("Proto Len: %x\n", arp_hdr.ar_pln);
printf("Command: %04x\n", arp_hdr.ar_op);
}
print_ARP_msg函数returns我这个:
Format HW: 0100
Format Proto: 0008
HW Len: 6
Proto Len: 4
Command: 0100
该结构的十六进制值为“00 01 08 00 06 04 00 01”,所以我不知道为什么它在 arp_hdr.ar_hrd 值中返回 0100。
另外,我制作了一个以十六进制打印结构的函数,以确保我做的是正确的,并且我能够检查所有字段是否正确分配。
PS:在发这个问题之前,我发现它打印的是正确的十六进制值,但是乱七八糟。可能与 little/big 字节序“差异”有关吗?
The hex values of the struct are "00 01 08 00 06 04 00 01", so I don't know why it's returning me 0100 in the arp_hdr.ar_hrd value.
看起来你的平台使用小端系统。
在记忆中似乎是 00 01
的内容被解释为 01 x 2^8 + 00
。换句话说,数字的十六进制表示是 0100
.
Could it be related to the little/big endian "difference"?
是的。如果您正在处理通过网络到达的数据包 - 并且您正在打印 ARP 数据包的字段,那么这正是您正在做的 - 您可能必须从字段的字节顺序转换为通过网络发送到您 运行.
机器上的字节顺序例如:
printf("Format HW: %04x\n", ntohs(arp_hdr.ar_hrd));
在这种特殊情况下,您可以在大端计算机(SPARC、System/3x0、z/Architecture、PowerPC/Power ISA 运行 AIX, PowerPC/Power ISA 运行 Mac OS X, 可能 PowerPC/Power ISA 运行 Linux, 等等),但是在小端机器(任何带有 x86 处理器的机器,包括 x86-64 处理器,可能是大多数 ARM 等)上,如果没有它,你就无法逃脱。
您可以在两种类型的处理器上使用它。
ARP 数据包与所有互联网协议数据包一样,以 "network order" 传输,即大端。例如,客户端必须使用 ntohs
(网络到主机的简称)将网络顺序转换为机器的本地字节顺序,而 htons
则相反。
详情见man byteorder
。