参照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