-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_util.cpp
More file actions
49 lines (42 loc) · 1.35 KB
/
bit_util.cpp
File metadata and controls
49 lines (42 loc) · 1.35 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
#include "bit_util.h"
#include "macro.h"
using namespace std;
namespace BitUtil {
void DumpHex(const char *bs, size_t bsLen, ostream &os, size_t pl) {
if (bs == nullptr || pl == 0) {
return;
}
os << "(len=" << bsLen << ")\n";
int lineCnt = (bsLen + pl - 1) / pl;
char tmp[8] = {0};
for (auto i = 0; i < lineCnt; ++i) {
string hexStr, visStr;
for (size_t j = 0; j < pl; ++j) {
size_t chIdx = i * pl + j;
bool isIn = chIdx < bsLen;
char ch = isIn ? bs[chIdx] : ' ';
if (isIn) {
snprintf(tmp, sizeof(tmp), "%02X ", ch & 0xFF);
hexStr += tmp;
visStr += IsByteVisible(ch) ? ch : '.';
} else {
hexStr += " ";
visStr += " ";
}
}
os << hexStr << " " << visStr.substr(0, visStr.size() - 1) << endl;
}
return;
}
string DumpHex(const char *bs, size_t bsLen, size_t pl) {
stringstream ss;
DumpHex(bs, bsLen, ss, pl);
return ss.str();
}
std::string DumpHex(const string& data, size_t pl) {
return DumpHex(data.data(), data.length(), pl);
}
bool IsByteVisible(char c) {
return c > 31 && c < 127;
}
}