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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <stdio.h>
#include <stdlib.h>
void print_buffer(unsigned char* buff, unsigned short buffer_len) {
// if (buff == NULL) { return; }
if (buffer_len > 0) {
int pos2;
unsigned short pos;
unsigned char* index = buff;
unsigned char c;
unsigned char part1, part2;
for(pos = 0; pos < buffer_len; pos += 16, index += 16) {
printf("\n");
for (pos2 = 0; ((pos2 < 16) && ((pos + pos2) < buffer_len)); ++pos2) {
c = (*(index + pos2));
part1 = (c & 0x0F);
part2 = ((c & 0xF0) >> 4);
if (part2 < 10) {
printf("%c", (part2 + '0'));
} else {
printf("%c", (part2 + 'A' - 10));
}
if (part1 < 10) {
printf("%c", (part1 + '0'));
} else {
printf("%c", (part1 + 'A' - 10));
}
if (pos2 <= 15) { printf(" "); }
}
if ((pos + 16) < buffer_len) {
printf(" | ");
} else {
for (pos2 = (pos + 16 - buffer_len); pos2 > 0; --pos2) { printf(" "); }
printf(" | ");
}
for (pos2 = 0; ((pos2 < 16) && ((pos + pos2) < buffer_len)); ++pos2) {
c = (*(index + pos2));
if ((c >= 32) && (c <= 126)) {
printf("%c", c);
} else {
if (c != '\0') { printf(" "); } else { printf("_"); }
}
}
}
printf("\n");
}
}
int main()
{
char str[] = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ligula lacus, tincidunt et accumsan et, tincidunt et quam. 123456789";
str[ 0] = '\0';
str[ 57] = '\0';
str[123] = '\0';
printf("str:\n>(start 0) %s\n>(start 1) %s\n>(start 58) %s\n\ndump hex:\n", str, (str + 1), (str + 58));
print_buffer(str, 134);
return EXIT_SUCCESS;
} |
Partager