-
Notifications
You must be signed in to change notification settings - Fork 200
/
hexdump.hpp
61 lines (55 loc) · 1.5 KB
/
hexdump.hpp
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
#ifndef HEXDUMP_HPP
#define HEXDUMP_HPP
#include <cctype>
#include <iomanip>
#include <ostream>
template <unsigned RowSize, bool ShowAscii>
struct CustomHexdump
{
CustomHexdump(const void* data, unsigned length) :
mData(static_cast<const unsigned char*>(data)), mLength(length) { }
const unsigned char* mData;
const unsigned mLength;
};
template <unsigned RowSize, bool ShowAscii>
std::ostream& operator<<(std::ostream& out, const CustomHexdump<RowSize, ShowAscii>& dump)
{
out.fill('0');
for (int i = 0; i < dump.mLength; i += RowSize)
{
out << std::setw(6) << std::hex << i << ": ";
for (int j = 0; j < RowSize; ++j)
{
if (i + j < dump.mLength)
{
out << std::hex << std::setw(2) << static_cast<int>(dump.mData[i + j]) << " ";
}
else
{
out << " ";
}
}
out << " ";
if (ShowAscii)
{
for (int j = 0; j < RowSize; ++j)
{
if (i + j < dump.mLength)
{
if (std::isprint(dump.mData[i + j]))
{
out << static_cast<char>(dump.mData[i + j]);
}
else
{
out << ".";
}
}
}
}
out << std::endl;
}
return out;
}
typedef CustomHexdump<16, true> Hexdump;
#endif // HEXDUMP_HPP