Skip to content

Instantly share code, notes, and snippets.

@codedust
Last active April 28, 2019 17:04
Show Gist options
  • Save codedust/d8c33354d5a5c273bfe1486d327dac43 to your computer and use it in GitHub Desktop.
Save codedust/d8c33354d5a5c273bfe1486d327dac43 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <cstring>
#include <iostream>
#include <iomanip>
void printb(const char* title, const unsigned char *buffer, size_t length) {
int indent = (strlen(title) > 6 ? ((strlen(title) + 2) / 6 + 1) * 6 : 7 + 2);
std::cout << title << ":" << std::string(indent - strlen(title) - 2, ' ');
for(size_t i = 0; i < length; i++) {
std::cout << ((i != 0 && i % 16 == 0) ? "\n" + std::string(indent, ' ') : " ");
std::cout << std::setfill('0') << std::hex << std::setw(2) << (int)buffer[i] << std::dec;
}
std::cout << '\n';
}
int main(void) {
const int BUFFER_SIZE = 40;
uint8_t* buffer = (uint8_t*) calloc(BUFFER_SIZE, sizeof(uint8_t));
printb("buffer", buffer, 40);
std::generate_n(buffer, BUFFER_SIZE, std::rand);
printb("random_buffer", buffer, 40);
}
/* outputs:
buffer: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
random_buffer: 67 c6 69 73 51 ff 4a ec 29 cd ba ab f2 fb e3 46
7c c2 54 f8 1b e8 e7 8d 76 5a 2e 63 33 9f c9 9a
66 32 0d b7 31 58 a3 5a
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment