在以下代码中:
struct ip_header {
unsigned char ip_hl:4;
unsigned char ip_ver:4;
unsigned char ip_dscp:6;
unsigned char ip_ecn:2;
unsigned int ip_len;
unsigned int ip_id;
...
};
...
const struct ip_header * ip_hdr = (const struct ip_header *)(buffer + ETHERNET_HEADER_SIZE);
...
printf("IP DSCP: %u\n", ip_hdr->ip_dcsp);
printf("IP Total Length: %u bytes\n", ip_hdr->ip_len);
...通常我有IP DSCP = 0和IP,总长度是某个数字< 65535。
每当我得到IP DSCP = 8时,总长度就变成一个7位数,即4231532字节。
但正如我们所知,无符号int范围在0到65535之间。
有人能解释一下这里发生了什么吗?IP DSCP = 8是什么意思?还是我错过了IP头长度和IP DSCP之间的某个字段?谢谢
发布于 2013-11-23 15:44:01
您对数据类型(如int )长度的假设是错误的。它们是定义的实现。
如果需要一个具有定义好的位数的变量,则可以使用
uint16_t
int16_t如果你是#include <stdint.h>的话。
https://stackoverflow.com/questions/20164009
复制相似问题