-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhex_dump.cpp
More file actions
67 lines (53 loc) · 1.65 KB
/
hex_dump.cpp
File metadata and controls
67 lines (53 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* hex_hump (char *data, int size, char *caption)
From: http://www.alexonlinux.com/hex-dump-functions
Modified to output in the Arduino IDE environment -- JR
*/
#include <string.h>
#include "Arduino.h" // Added reference to Arduino functions -- JR
#include "hex_dump.h"
extern "C" {
void hex_dump(char *data, int size, char *caption)
{
int i; // index in data...
int j; // index in line...
char temp[8];
char buffer[128];
char *ascii;
memset(buffer, 0, 128);
Serial.printf("\n---------> %s <--------- (%d bytes from %p)\n", caption, size, data);
// Printing the ruler...
Serial.printf(" +0 +4 +8 +c 0 4 8 c \n");
// Hex portion of the line is 8 (the padding) + 3 * 16 = 52 chars long
// We add another four bytes padding and place the ASCII version...
ascii = buffer + 58;
memset(buffer, ' ', 58 + 16);
buffer[58 + 16] = '\n';
buffer[58 + 17] = '\0';
buffer[0] = '+';
buffer[1] = '0';
buffer[2] = '0';
buffer[3] = '0';
buffer[4] = '0';
for (i = 0, j = 0; i < size; i++, j++)
{
if (j == 16)
{
Serial.printf("%s", buffer);
memset(buffer, ' ', 58 + 16);
sprintf(temp, "+%04x", i);
memcpy(buffer, temp, 5);
j = 0;
}
sprintf(temp, "%02x", 0xff & data[i]);
memcpy(buffer + 8 + (j * 3), temp, 2);
if ((data[i] > 31) && (data[i] < 127))
ascii[j] = data[i];
else
ascii[j] = '.';
}
if (j != 0)
Serial.printf("%s", buffer);
Serial.printf("\t\t ======= END OF BUFFER DUMP ======= \n\n");
Serial.flush();
}
}