hexdump.cpp
Go to the documentation of this file.
1 
2 #include <cassert>
3 #include <iomanip>
4 #include <iostream>
5 #include <sstream>
6 #include <string>
7 
8 #include "common.hpp"
9 
10 using namespace std;
11 
12 void hexdump(const void *data, int dataLen) {
13  const char *const start = static_cast<const char *>(data);
14 
15  int it = 0;
16 
17  stringstream all;
18 
19  cout << "Hexdump of: " << data << endl;
20  while (dataLen > 0) {
21  unsigned int lineLength = min((int)16, dataLen);
22  const char *line = start + it;
23 
24  stringstream ascii;
25  stringstream hexDump;
26 
27  ascii << setfill('0') << setw(2) << hex << it << " : ";
28 
29  for (unsigned int i = 0; i < lineLength; i++) {
30  char c = static_cast<char>(line[i]);
31  unsigned int varInt = 0;
32  varInt |= static_cast<uint8_t>(c);
33 
34  if (isprint(varInt) != 0) {
35  ascii << static_cast<char>(line[i]);
36  } else {
37  ascii << '.';
38  }
39 
40  hexDump << " " << setfill('0') << setw(2) << hex << uppercase << varInt;
41  }
42 
43  for (int i = lineLength; i < 16; i++) {
44  ascii << " ";
45  }
46 
47  all << ascii.str();
48  all << hexDump.str();
49  all << endl;
50 
51  dataLen -= 16;
52  it += lineLength;
53  }
54 
55  cout << all.str();
56 }
57 
58 void hexdumpHexOnly(const void *data, int dataLen) {
59  const char *const start = static_cast<const char *>(data);
60 
61  int it = 0;
62 
63  stringstream all;
64 
65  cout << "Hexdump of: " << data << endl;
66  while (dataLen > 0) {
67  unsigned int lineLength = min((int)16, dataLen);
68  const char *line = start + it;
69 
70  for (unsigned int i = 0; i < lineLength; i++) {
71  char c = static_cast<char>(line[i]);
72  unsigned int varInt = 0;
73  varInt |= static_cast<uint8_t>(c);
74 
75  all << " " << setfill('0') << setw(2) << hex << uppercase << varInt;
76  }
77 
78  all << endl;
79 
80  dataLen -= 16;
81  it += lineLength;
82  }
83 
84  cout << all.str();
85 }
void hexdumpHexOnly(const void *data, int dataLen)
Definition: hexdump.cpp:58
void hexdump(const void *data, int dataLen)
Dump hex data.
Definition: hexdump.cpp:12