forked from charlieh0tel/google-libusb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghexdump.cc
52 lines (46 loc) · 1.05 KB
/
ghexdump.cc
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
// Copyright 2012 Google Inc. All Rights Reserved.
//
#include "ghexdump.h"
#include <sstream>
#include <iomanip>
#include <stddef.h>
#include <stdint.h>
#include <cstring>
#include <glog/logging.h>
namespace glibusb {
std::string Dump(const void *buf, size_t n) {
const int kBytesPerRow = 16;
const uint8_t *p = static_cast<const uint8_t *>(CHECK_NOTNULL(buf));
std::stringstream dump;
for (uint32_t i = 0; i < n;) {
dump << "0x"
<< std::hex << std::setw(8) << std::setfill('0') << i
<< ": ";
for (int j = 0; j < kBytesPerRow; ++j) {
auto o = i + j;
if (o < n) {
auto b = p[o];
dump << std::hex << std::setw(2) << std::setfill('0') << int(b) << ' ';
} else {
dump << " ";
}
}
dump << " | ";
for (int j = 0; j < kBytesPerRow; ++j) {
auto o = i + j;
if (o == n) {
break;
}
auto b = p[o];
if (b > ' ' && b <= '~') {
dump << char(b);
} else {
dump << '.';
}
}
dump << std::endl;
i += kBytesPerRow;
}
return dump.str();
}
} // namespace glibusb