/
hexdump.hpp
68 lines (64 loc) · 2.15 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
62
63
64
65
66
67
68
#pragma once
#include <locale>
#include <ostream>
#include <string>
namespace cu
{
/// Prints a variable or array in hex dump format into a std::basic_ostream output stream
///
/// @example Here's how it's used:
/// @code
/// std::string r;
///
/// for (int i = 0; i != 100; ++i)
/// r += rand() % 256;
///
/// hexDump(r.c_str(), r.size(), std::cout);
/// @endcode
///
/// Output:
///
/// 0000 : )#...l..R.I..... 29 23 BE 84 E1 6C D6 AE 52 90 49 F1 F1 BB E9 EB
/// 0010 : ...<..>.$^....G. B3 A6 DB 3C 87 0C 3E 99 24 5E 0D 1C 06 B7 47 DE
/// 0020 : ..M.C.....Z}.8%. B3 12 4D C8 43 BB 8B A6 1F 03 5A 7D 09 38 25 1F
/// 0030 : ].....E;.......2 5D D4 CB FC 96 F5 45 3B 13 0D 89 0A 1C DB AE 32
/// 0040 : .P.@x6..I2..}I. 20 9A 50 EE 40 78 36 FD 12 49 32 F6 9E 7D 49 DC
/// 0050 : .O..D@f.k.0.2;." AD 4F 14 F2 44 40 66 D0 6B C4 30 B7 32 3B A1 22
/// 0060 : .".. F6 22 91 9D
template<class elem, class traits>
inline void hexDump( const void* data,
std::size_t length,
std::basic_ostream<elem, traits>& stream,
std::size_t width = 16 )
{
const char* const start = static_cast<const char*>(data);
const char* const end = start + length;
const char* line = start;
const auto locale = stream.getloc();
while (line != end)
{
stream.width(4);
stream.fill('0');
stream << std::hex << std::uppercase;
stream << line - start << " : ";
std::size_t lineLength = std::min(width, static_cast<std::size_t>(end - line));
for (const char* next = line; next != line + lineLength; ++next)
{
char ch = *next;
stream << ( !std::isprint( ch, locale ) ? '.' : ch );
}
stream << std::string(width - lineLength, ' ');
for (const char* next = line; next != line + lineLength; ++next)
{
stream << " ";
stream.width(2);
stream.fill('0');
stream << std::hex
<< std::uppercase
<< static_cast<int>(static_cast<unsigned char>(*next));
}
stream << std::endl;
line += lineLength;
}
}
} // namespace cu