mirror of
https://github.com/Zygo/bees.git
synced 2025-05-18 05:45:45 +02:00
There is a hexdump template in fs. Move hexdump to its own header, then ByteVector can use it too. Signed-off-by: Zygo Blaxell <bees@furryterror.org>
37 lines
727 B
C++
37 lines
727 B
C++
#ifndef CRUCIBLE_HEXDUMP_H
|
|
#define CRUCIBLE_HEXDUMP_H
|
|
|
|
#include "crucible/string.h"
|
|
|
|
#include <ostream>
|
|
|
|
namespace crucible {
|
|
using namespace std;
|
|
|
|
template <class V>
|
|
ostream &
|
|
hexdump(ostream &os, const V &v)
|
|
{
|
|
os << "V { size = " << v.size() << ", data:\n";
|
|
for (size_t i = 0; i < v.size(); i += 8) {
|
|
string hex, ascii;
|
|
for (size_t j = i; j < i + 8; ++j) {
|
|
if (j < v.size()) {
|
|
uint8_t c = v[j];
|
|
char buf[8];
|
|
sprintf(buf, "%02x ", c);
|
|
hex += buf;
|
|
ascii += (c < 32 || c > 126) ? '.' : c;
|
|
} else {
|
|
hex += " ";
|
|
ascii += ' ';
|
|
}
|
|
}
|
|
os << astringprintf("\t%08x %s %s\n", i, hex.c_str(), ascii.c_str());
|
|
}
|
|
return os << "}";
|
|
}
|
|
};
|
|
|
|
#endif // CRUCIBLE_HEXDUMP_H
|