Need help understanding this.....

This is a discussion on Need help understanding this..... within the C Programming forums, part of the General Programming Boards category; //got this code from a book. Code: void dump(const unsigned char *data_buffer, const unsigned int length) { unsigned char byte; ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's a replacement for the \t (tab) formatting character in printf().

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    In the i%16 expression the % signifies modulo operation.

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    ok....
    char *data = "AAAAA";
    int size = 5;
    dump(data, size);

    This outputs

    41 41 41 41 41 | AAAAA

    But why?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dr3w2k View Post
    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....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Understanding Some Basics
    By TheProfessor in forum C Programming
    Replies: 2
    Last Post: 10-02-2010, 12:58 PM
  2. Understanding forward declarations of classes, pimpl
    By Boxknife in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2010, 02:29 AM
  3. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  4. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 06:53 AM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21