Created
April 11, 2020 22:25
-
-
Save qookei/a5165cc0a1d9943c1b40c38377cca190 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constexpr size_t bytes_per_line = 16; | |
void buffer_pretty_print(const void *buf, size_t size) { | |
uintptr_t addr = reinterpret_cast<uintptr_t>(buf); | |
for (size_t i = 0; i < (size + bytes_per_line - 1) / bytes_per_line; i++) { | |
uint8_t buf[bytes_per_line]; | |
auto off = i * bytes_per_line; | |
size_t n = std::min(bytes_per_line, size - off); | |
std::memcpy( | |
buf, | |
reinterpret_cast<const void *>(addr + off), | |
n | |
); | |
printf(" %016lx: ", addr + off); | |
for (size_t j = 0; j < n; j++) | |
printf("%02hhx ", buf[j]); | |
for (size_t j = 0; j < bytes_per_line - n; j++) | |
printf(" "); | |
for (auto &c : buf) | |
c = std::isprint(c) ? c : '.'; | |
printf("| %.*s\n", static_cast<int>(n), buf); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment