参照gdb的风格, 逐字节打印,同时打印10进制无符号和16进制,一行打印8字节
其中
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
int scnprintf(char *buf, size_t size, const char *fmt, ...)
两个函数是从Linux内核源码复制的
直接上完整代码:
#include <cstdio>
#include <cstdarg>
#define unlikely(expr) __builtin_expect(!!(expr), 0)
#define likely(expr) __builtin_expect(!!(expr), 1)
/**
* vscnprintf - Format a string and place it in a buffer
* @buf: The buffer to place the result into
* @size: The size of the buffer, including the trailing null space
* @fmt: The format string to use
* @args: Arguments for the format string
*
* The return value is the number of characters which have been written into
* the @buf not including the trailing '\0'. If @size is == 0 the function
* returns 0.
*
* If you're not already dealing with a va_list consider using scnprintf().
*
* See the vsnprintf() documentation for format string extensions over C99.
*/
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
int i;
i = vsnprintf(buf, size, fmt, args);
if (likely(i < size))
{
return i;
}
if (size != 0)
{
return size - 1;
}
return 0;
}
/**
* scnprintf - Format a string

本文介绍了一种使用C++编程的方式,参照GDB调试器的风格,逐字节打印内存数据,同时展示10进制无符号和16进制表示。代码中引用了两个来自Linux内核的函数,并给出了完整的实现代码,运行结果显示每行打印8字节的数据。
最低0.47元/天 解锁文章
918

被折叠的 条评论
为什么被折叠?



