21 std::string
hexdump(
const void*
const data,
size_t size) {
22 const unsigned char*
const cdata =
23 static_cast<const unsigned char*
>(data);
28 static const char xdigits[16] = {
29 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
30 '8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' 33 std::string::iterator oi = out.begin();
34 for (
const unsigned char* si = cdata; si != cdata + size; ++si) {
35 *oi++ = xdigits[(*si & 0xF0) >> 4];
36 *oi++ = xdigits[(*si & 0x0F)];
42 std::string
hexdump(
const std::string& str) {
43 return hexdump(str.data(), str.size());
46 std::string
hexdump(
const std::vector<char>& data) {
47 return hexdump(data.data(), data.size());
50 std::string
hexdump(
const std::vector<uint8_t>& data) {
51 return hexdump(data.data(), data.size());
55 const std::string& str,
const std::string& var_name) {
57 std::ostringstream header;
58 header <<
"const uint8_t " << var_name <<
"[" << str.size() <<
"] = {\n";
60 static const int perline = 16;
62 std::string out = header.str();
63 out.reserve(out.size() + (str.size() * 5) - 1 + (str.size() / 16) + 4);
65 static const char xdigits[16] = {
66 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
67 '8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' 70 std::string::size_type ci = 0;
72 for (std::string::const_iterator si = str.begin(); si != str.end();
76 out += xdigits[(*si & 0xF0) >> 4];
77 out += xdigits[(*si & 0x0F)];
79 if (ci + 1 < str.size()) {
82 if (ci % perline == perline - 1)
95 std::string
hexdump_lc(
const void*
const data,
size_t size) {
96 const unsigned char*
const cdata =
97 static_cast<const unsigned char*
>(data);
100 out.resize(size * 2);
102 static const char xdigits[16] = {
103 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
104 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' 107 std::string::iterator oi = out.begin();
108 for (
const unsigned char* si = cdata; si != cdata + size; ++si) {
109 *oi++ = xdigits[(*si & 0xF0) >> 4];
110 *oi++ = xdigits[(*si & 0x0F)];
134 for (std::string::const_iterator si = str.begin(); si != str.end(); ++si) {
178 default:
throw std::runtime_error(
"Invalid string for hex conversion");
183 throw std::runtime_error(
"Invalid string for hex conversion");
225 default:
throw std::runtime_error(
"Invalid string for hex conversion");
228 out +=
static_cast<char>(c);
std::string hexdump_lc(const void *const data, size_t size)
Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
std::string parse_hexdump(const std::string &str)
Read a string as a sequence of hexadecimal pairs.
std::string hexdump_sourcecode(const std::string &str, const std::string &var_name)
Dump a (binary) string into a C source code snippet.
std::string hexdump(const void *const data, size_t size)
Dump a (binary) string as a sequence of uppercase hexadecimal pairs.