Here are 3 fixups
void hexDump( unsigned char *buf, size_t size ) {
for( size_t i = 0; i < size; i++ )
printf( "%02x%c", buf[ i ], " \n"[ i % 4 ] );
}
1 - buf
should be treated like hex. Hexadecimal is unsigned
2 - size_t i
Don't compare an int
to a size_t
type
3 - indexing into a 4 byte string (3 x SP and 1 x LF) eliminates the if()
You could even go on to add the snazzy address column on the left:
#include <stdio.h>
#include <string.h>
void hexDump( unsigned char *buf, size_t size ) {
for( size_t i = 0; i < size; i++ ) {
if( i % 4 == 0 )
printf( "%04X ", i );
printf( "%02x%c", buf[ i ], " \n"[ i % 4 ] );
}
}
int main() {
char *str = "The quick brown fox jumps over the lazy dogs";
hexDump( (unsigned char*) str, strlen( str ) + 1 );
return 0;
}
Output
0000 54 68 65 20
0004 71 75 69 63
0008 6b 20 62 72
000C 6f 77 6e 20
0010 66 6f 78 20
0014 6a 75 6d 70
0018 73 20 6f 76
001C 65 72 20 74
0020 68 65 20 6c
0024 61 7a 79 20
0028 64 6f 67 73
002C 00
Or, the ACTUAL memory address with 8 / line. (Notice now 7xSP + 1xLF)
void hexDump( unsigned char *buf, size_t size ) {
for( size_t i = 0; i < size; i++ ) {
if( i % 8 == 0 )
printf( "%p ", buf + i );
printf( "%02x%c", buf[ i ], " \n"[ i % 8 ] );
}
}
/* Output */
00422B10 54 68 65 20 71 75 69 63
00422B18 6b 20 62 72 6f 77 6e 20
00422B20 66 6f 78 20 6a 75 6d 70
00422B28 73 20 6f 76 65 72 20 74
00422B30 68 65 20 6c 61 7a 79 20
00422B38 64 6f 67 73 00