- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-debug.h
More file actions
Latest commit
67 lines (58 loc) · 1.46 KB
/
Copy pathcpp-debug.h
File metadata and controls
67 lines (58 loc) · 1.46 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
#ifndef CPP_DEBUG_H
#defineCPP_DEBUG_H
#include<iostream>
#include<sstream>
#include<iomanip>
//
// Return a string with a hex dump representation of the provided bytes
//
std::string hexDump(constunsignedchar* data, unsignedint len) {
std::ostringstream ostr;
ostr << "===========================================================================\n";
for (unsigned i = 0; i < len; i += 16) {
// index
ostr << std::setw(5) << std::setfill('0') << std::dec << i << "";
// hex representation
for (int j = 0; j < 16; j++) {
if (i + j < len) {
ostr << std::setw(2) << std::hex << std::uppercase << int(data[i + j]) << "";
}
else {
ostr << "";
}
if (j == 7) {
ostr << "";
}
}
ostr << "";
// char representation
for (int j = 0; j < 16; j++) {
if (i + j < len) {
char ch = data[i + j];
if (ch >= 32 && ch < 127) {
ostr << ch;
}
else {
ostr << ".";
}
}
else {
ostr << "";
}
if (j == 7) {
ostr << "";
}
}
ostr << "\n";
}
ostr << "Length = " << std::setw(5) << std::dec << len;
ostr << " ============================================================\n\n";
return ostr.str();
}
//
// Print a hex dump representation of the given data to stdout.
//
voidprintHexDump(constunsignedchar* data, unsignedint len) {
std::cout << hexDump(data, len);
}
#endif