首页 > 解决方案 > 如何将 8 字节空指针(包括字符)打印为十六进制

问题描述

我目前正在尝试打印由 SSH Wireshark 解析器捕获的数据。我调试了我需要的 void 指针数组,在调试器中,第一个索引如下所示:

    [0] 0xa9ac1405c4040000  void *
    [1] 0xc4c2f211de8a3e38  void *

但是,当我尝试打印内容时,我得到的值如下所示: C4040000 DE8A3E38 这意味着数据仅在前 4 个字节之后打印。

我现在的问题是,我怎样才能得到整个 void 指针。我的代码如下所示:

为了澄清。我为获取上述数据而设置的断点位于最后显示的行中。

编辑:我将 tvb_memcpy 的代码添加到示例中。tvb_captured_length 返回的长度是数据包的长度,应该是正确的。

gint length = tvb_captured_length(tvb);
size_t length_2 = length;
unsigned char target[2000];

tvb_memcpy(tvb, target, offset, length_2);
g_print("data: ");
int i;
for (i = 0; i < length; ++i) {
    g_print(" %02X", (unsigned char *)target[i]);
}
g_print("\n");

void *
tvb_memcpy(tvbuff_t *tvb, void *target, const gint offset, size_t length)
{
guint   abs_offset = 0, abs_length = 0;

DISSECTOR_ASSERT(tvb && tvb->initialized);

/*
 * XXX - we should eliminate the "length = -1 means 'to the end
 * of the tvbuff'" convention, and use other means to achieve
 * that; this would let us eliminate a bunch of checks for
 * negative lengths in cases where the protocol has a 32-bit
 * length field.
 *
 * Allowing -1 but throwing an assertion on other negative
 * lengths is a bit more work with the length being a size_t;
 * instead, we check for a length <= 2^31-1.
 */
DISSECTOR_ASSERT(length <= 0x7FFFFFFF);
check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);

if (tvb->real_data) {
    return memcpy(target, tvb->real_data + abs_offset, abs_length);
}

if (tvb->ops->tvb_memcpy)
    return tvb->ops->tvb_memcpy(tvb, target, abs_offset, abs_length);

/*
 * If the length is 0, there's nothing to do.
 * (tvb->real_data could be null if it's allocated with
 * a size of length.)
 */
if (length != 0) {
    /*
     * XXX, fallback to slower method
     */
    DISSECTOR_ASSERT_NOT_REACHED();
}
return NULL;
}

编辑 2:我将 void * 更改为 unsigned char * 指针并且它有效。感谢大家!

标签: cpointersprinting

解决方案


使用%p格式说明符。

#include <stdio.h>

int main()
{
        int a;
        void *p;

        p = &a;
        printf("p=%p (as ptr), p=%02X\n", p, p);

        return (0);
}

输出(在我的系统上):

p=0x7fffffffeb44 (as ptr), p=FFFFEB44

推荐阅读