c - 如何将 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 * 指针并且它有效。感谢大家!
解决方案
使用%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
推荐阅读
- typescript - 如何使自定义 cypress 命令与 typescript 一起使用
- javascript - 获取每个点击的数据属性并输出 ICS
- javascript - 使用来自节点 js 的 ajax 变量
- php - Laravel 表迁移(未创建表)
- sql - Laravel 多对多关系计数
- buildroot - ubuntu cp:无法统计
.tar.gz':没有这样的文件或目录 - python - 使用多个程序访问串口
- java - 将 Flyway 添加到已投入生产的项目中
- postgresql - 在 postgres 中匿名化 JSONB 的更好方法?
- c# - 总结在列表属性列表中