-
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.46 KB
/
hex_dump.cpp
File metadata and controls
67 lines (53 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
#include "hex_dump.hpp"
#include <cassert>
#include <ostream>
hex_dump::hex_dump( std::ostream& output )
: output_( output )
{
}
static std::uint8_t nibble( std::uint8_t val )
{
assert(val < 16);
return val < 10 ? '0' + val : 'a' + (val - 10);
}
static void print_hex(std::ostream& out, std::uint8_t value)
{
out << nibble(value >> 4) << nibble(value & 0xf);
}
static std::uint8_t as_printable(std::uint8_t maybe_printable)
{
return maybe_printable > 31 && maybe_printable < 127
? maybe_printable
: '.';
}
void hex_dump::dump( const interface& config, const std::uint8_t* begin, std::size_t size )
{
static const std::size_t page_width = 16;
std::size_t char_cnt = page_width;
const std::uint8_t* line_start = begin;
line_header( config );
while ( size )
{
print_hex( output_, *begin );
output_ << ' ';
--size;
++begin;
--char_cnt;
if ( char_cnt == 0 || size == 0 )
{
// fill rest of the line
for ( ; char_cnt != 0; --char_cnt)
output_ << " ";
for ( ; line_start != begin; ++line_start )
output_ << as_printable( *line_start );
output_ << '\n';
if ( size )
line_header( config );
char_cnt = page_width;
}
}
}
void hex_dump::line_header( const interface& config )
{
output_ << config.alias() << ": ";
}