在嵌入式开发中,不管是驱动调试还是应用协议开发,经常需要十六进制打印数据,供调试使用。以下是C实现的一个十六进制输出的函数,感觉挺实用的,供参考
#define BIN2CHAR(ch) (((ch) > ' ' && (ch) <= '~') ? (ch) : '.')
void dumphex(unsigned char *buf, int len)
{
char membuf[1024] = {0};
unsigned char *ptr = (unsigned char *) buf;
char *ibuf = membuf;
int i, j;
int len1, len2;
len1 = len / 16 * 16;
len2 = len % 16;
ibuf[0] = 0;
for (i = 0; i < len1; i += 16)
{
ibuf = membuf;
sprintf(ibuf, "0x%08X ", (int) buf + i);
ibuf += 12;
for (j = 0; j < 16; j++)
{
sprintf(ibuf, "%02X ", ptr[j]);
ibuf += 3;
}
sprintf(ibuf, " ");
ibuf += 2;
for (j = 0; j < 16; j++)
{
sprintf(ibuf, &