# Thread: Need help understanding this.....

1. ## Need help understanding this.....

//got this code from a book.
Code:
```void dump(const unsigned char *data_buffer, const unsigned int length) {
unsigned char byte;
unsigned int i, j;
for(i=0; i < length; i++) {
byte = data_buffer[i];
printf("%02x ", data_buffer[i]);  // display byte in hex
if(((i%16)==15) || (i==length-1)) {
for(j=0; j < 15-(i%16); j++)
printf("   ");
printf("| ");
for(j=(i-(i%16)); j <= i; j++) {  // display printable bytes from line
byte = data_buffer[j];
if((byte > 31) && (byte < 127)) // outside printable char range
printf("%c", byte);
else
printf(".");
}
printf("\n"); // end of the dump line (each line 16 bytes)
} // end if
} // end for
}```
Can someone explain this part of code....
if(((i%16)==15) || (i==length-1)) {
for(j=0; j < 15-(i%16); j++)
also...

for(j=(i-(i%16)); j <= i; j++) {

Most of this makes sense, it's the i%16 part that's throwing me off.
Any help appreciated.

2. It's a replacement for the \t (tab) formatting character in printf().

3. In the i%16 expression the % signifies modulo operation.

Jim

4. ok....
char *data = "AAAAA";
int size = 5;
dump(data, size);

This outputs

41 41 41 41 41 | AAAAA

But why?

5. Why what? Why is 'A' 41 in hex? Because that's what your current code page has it as. Here's a fine example: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

Quzah.

6. Originally Posted by dr3w2k
ok....
char *data = "AAAAA";
int size = 5;
dump(data, size);

This outputs

41 41 41 41 41 | AAAAA

But why?
Because 41 is the hexidecimal code for the letter A....

Table of ASCII Characters

Become the CPU.... Follow the code step by step... keep track of the values of the variables, run the loops on paper... you'll figure it out. In fact this is just the kind of thing that teaches lessons that don't get forgotten....