Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分析位操作数c+;的无符号字符指针+;_C++_Linux_Pointers_Binary - Fatal编程技术网

C++ 分析位操作数c+;的无符号字符指针+;

C++ 分析位操作数c+;的无符号字符指针+;,c++,linux,pointers,binary,C++,Linux,Pointers,Binary,我有一个指向原始以太网数据包的指针,我想打印它的IP版本。我在Linux客户端上使用C++。 IP版本与IHL(互联网报头长度)一起存储在数据包报头的第14字节。我有一个函数可以打印IP报头中的所有字段,但由于IP版本与IHL一起存储在第14个字节中,因此我必须打印第14个字节中的前4位,我不知道如何执行此操作。我想打印IP版本和IHL 我的功能如下: int PrintIPHeader(unsigned char *eth_head) { unsigned char *ip_head

我有一个指向原始以太网数据包的指针,我想打印它的IP版本。我在Linux客户端上使用C++。 IP版本与IHL(互联网报头长度)一起存储在数据包报头的第14字节。我有一个函数可以打印IP报头中的所有字段,但由于IP版本与IHL一起存储在第14个字节中,因此我必须打印第14个字节中的前4位,我不知道如何执行此操作。我想打印IP版本和IHL

我的功能如下:

int PrintIPHeader(unsigned char *eth_head)
{
    unsigned char *ip_head = eth_head;
    int j;

    printf("---Start of IP Header---");
    printf("\nVersion\n");
    for(j = 0; j < 1; j++)
    {
        printf("%d", *(ip_head+j));
    }

    printf("\nIHL\n");
    for(j = 0; j < 1; j++)
    {
        printf("%d", *(ip_head+j));
    }

    printf("\nToS\n");
    for(j = 1; j < 2; j++)
    {
        printf("%d", *(ip_head+j));
    }

    printf("\nTotal Length\n");
    for(j = 2; j < 4; j++)
    {
        printf("%d", *(ip_head+j));
    }

    ...
    and so and so on with all the fields of an IP header 
    (I know the for-loop is unnecessary in some of the cases)
}
但我得到一个错误“类型为'unsigned char*'和'int'的无效操作数转换为二进制'operator>>'


有人找到解决方案吗?

如果您使用此命令

printf(ip_head[14] >> 4); 
这些位不会被永久覆盖。 你可以把这种行为比作

printf(a + 4);

其中a仍然像以前一样保留该值,但printf被交给了一个a+4的值。

无关:这样做的循环:
for(j=0;j<1;j++)
对我来说似乎毫无意义。然而,这似乎是一个反复出现的主题在这个来源。奇怪。@WhozCraig我只是快速复制粘贴了大代码段,结果写了完全不需要的循环,我也在代码段中写了。没有必要真正指出这一点。
ip\u head[14]
为什么会是指针?根据显示的代码,它是一个类型为
unsigned char
@usr的值,我不太明白你在说什么?您必须详细说明..@Zeliax错误消息说它是一个
无符号字符*
。你不同意吗?为什么会这样说?
printf(a + 4);