-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.cpp
More file actions
49 lines (45 loc) · 1.09 KB
/
Utils.cpp
File metadata and controls
49 lines (45 loc) · 1.09 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 "Utils.hpp"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
std::string format(const std::string& fmt, ...)
{
int size = ((int)fmt.size()) * 2 + 50;
std::string str;
va_list ap;
while (1) {
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)str.data(), size, fmt.c_str(), ap);
va_end(ap);
if (n > -1 && n < size) {
str.resize(n);
return str;
}
if (n > -1)
size = n + 1;
else
size *= 2;
}
return str;
}
void hexdump(const unsigned char *data, size_t length)
{
char asciibuf[18];
asciibuf[16] = '\0';
for (unsigned int index = 0; index < length; index++) {
if((index % 16) == 0) {
printf("%08X ", index);
}
if((index % 8) == 0) {
putchar(' ');
}
printf("%02X ", data[index]);
asciibuf[index % 16] = isprint(data[index])?data[index]:'.';
if((index % 16) == 15) {
printf(" %s\n", asciibuf);
}
}
printf("\n");
}