38 lines
1.1 KiB
C++
38 lines
1.1 KiB
C++
|
|
#include "hexdump.h"
|
||
|
|
|
||
|
|
void hexdump_to_fd(int fd, const char *data, uint16_t len)
|
||
|
|
{
|
||
|
|
uint16_t i = 0;
|
||
|
|
uint16_t used = 0;
|
||
|
|
uint16_t offset = 0;
|
||
|
|
|
||
|
|
#define LINE_LEN 80
|
||
|
|
char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
|
||
|
|
|
||
|
|
dprintf(fd, "dump data at [%p], len=%u\n", data, len);
|
||
|
|
while (offset < len)
|
||
|
|
{
|
||
|
|
/* format the line in the buffer, then use printf to usedput to screen */
|
||
|
|
used = snprintf(line, LINE_LEN, "%08X:", offset);
|
||
|
|
for (i = 0; ((offset + i) < len) && (i < 16); i++)
|
||
|
|
{
|
||
|
|
used += snprintf(line + used, LINE_LEN - used, " %02X", (data[offset + i] & 0xff));
|
||
|
|
}
|
||
|
|
for (; i <= 16; i++)
|
||
|
|
{
|
||
|
|
used += snprintf(line + used, LINE_LEN - used, " | ");
|
||
|
|
}
|
||
|
|
for (i = 0; (offset < len) && (i < 16); i++, offset++)
|
||
|
|
{
|
||
|
|
unsigned char c = data[offset];
|
||
|
|
if ((c < ' ') || (c > '~'))
|
||
|
|
{
|
||
|
|
c = '.';
|
||
|
|
}
|
||
|
|
used += snprintf(line + used, LINE_LEN - used, "%c", c);
|
||
|
|
}
|
||
|
|
dprintf(fd, "%s\n", line);
|
||
|
|
}
|
||
|
|
dprintf(fd, "\n");
|
||
|
|
}
|