-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHexDump.cpp
85 lines (77 loc) · 2.8 KB
/
HexDump.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* HexDump.hpp
* Hex memory dump utility functions for Arduino. 0x00 and 0xFF are printed as spaces.
*
* Sample output:
* 0x0000: 0xF1 0x81 0x82 0x00 0x08 0x02 0x00 0x27 0xFF 0xFF 0x0E 0xB3 0x81 0xFC 0x9B 0x47 ... .. ' .....G
* 0x0020: 0x00 0x00 0x00 0x00 0x20 0x65 0x00 0x0F 0xBE 0xEB 0x9B 0x98 0x2C 0xF1 0x08 0x2C e .....,..,
*
* Copyright (C) 2022 Armin Joachimsmeyer
* Email: [email protected]
*
* This file is part of Arduino-Utils https://github.com/ArminJo/Arduino-Utils.
*
* ArduinoUtils is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
*/
#ifndef _HEX_DUMP_HPP
#define _HEX_DUMP_HPP
#include <Arduino.h>
#define BYTES_PER_LINE 16
/*
* Print with leading space and padded with 0
*/
void printBytePaddedHex(uint8_t aHexValueToPrint) {
Serial.print(F(" 0x"));
if (aHexValueToPrint < 0x10) {
Serial.print('0');
}
Serial.print(aHexValueToPrint, HEX);
}
void printWordPaddedHex(uint16_t aHexValueToPrint) {
Serial.print(F("0x"));
if (aHexValueToPrint < 0x1000) {
Serial.print('0');
}
if (aHexValueToPrint < 0x100) {
Serial.print('0');
}
if (aHexValueToPrint < 0x10) {
Serial.print('0');
}
Serial.print(aHexValueToPrint, HEX);
}
void printMemoryHexDump(uint8_t *aMemory, size_t aSizeOfMemoryToPrint) {
for (uint16_t tIndex = 0; tIndex < aSizeOfMemoryToPrint; tIndex += BYTES_PER_LINE) {
printWordPaddedHex(tIndex);
Serial.print(F(": "));
for (uint_fast8_t i = 0; i < BYTES_PER_LINE; i++) {
printBytePaddedHex(aMemory[tIndex + i]);
}
Serial.print(F(" "));
for (uint_fast8_t i = 0; i < BYTES_PER_LINE; i++) {
// if(isalnum(tIndex+i)){ // requires 40 bytes more program space
if (' ' <= aMemory[tIndex + i] && aMemory[tIndex + i] <= '~') {
Serial.print((char)aMemory[tIndex + i]);
} else if (aMemory[tIndex + i] != 0x00 && aMemory[tIndex + i] != 0xFF) {
// for non printable characters except 0 and FF
Serial.print('.');
} else {
Serial.print(' ');
}
}
tIndex += BYTES_PER_LINE;
Serial.println();
}
}
#endif // _HEX_DUMP_HPP