Jump to content

Need help in understanding a loop

- - - - -

  • Please log in to reply
1 reply to this topic

#1
lionaneesh

lionaneesh

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
// Dumps raw memory in hex byte and printable split format

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 is 16 bytes)

      } // End if

   } // End for

}

Hey guys i Found this code on the Net somewhere.
What it does it is written on the top of it .

I am facing problems understanding this function :-

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(".");

I am not getting what is exactly happening in this loop!!!

Need you help guys please explain me this..

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It looks like it's processing 16 bytes of data at a time.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users