如何将8字节的空指针(包括字符)打印为十六进制

时间:2019-07-13 09:46:33

标签: c pointers printing

我目前正在尝试打印由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 *更改为未签名的char *指针,并且它起作用了。谢谢大家!

2 个答案:

答案 0 :(得分:1)

为什么将数据复制到void指针数组中?

您可能应该只使用unsigned char。但是,我无法告诉您您使用tvbtvb_captured_length()tvb_memcpy ...是正确的。

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", target[i]);
}
g_print("\n");

答案 1 :(得分:1)

使用%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