C++ How-To: Print a Buffer

I was recently writing a command line application in C++ that parses raw binary.  I thought it would be really nice to be able to print different parts of memory to the screen as the program runs.  I’ve included well-commented code and a usage example.

   1: //needed for printf()

   2: #include <stdio.h>

   3:  

   4: //needed for strlen()

   5: #include <string.h>

   6:  

   7: // prints the contents of memory in hex and ascii.

   8: // starts at the location of the pointer "start"

   9: // prints "length" bytes of memory.

  10: void Print_Memory(const unsigned char * start, unsigned int length)

  11: {

  12:     //create row, col, and i.  Set i to 0

  13:     int row, col, i = 0;

  14:  

  15:     //iterate through the rows, which will be 16 bytes of memory wide

  16:     for(row = 0; (i + 1) < length; row++)

  17:     {

  18:         //print hex representation

  19:         for(col = 0; col<16; col++)

  20:         {

  21:             //calculate the current index

  22:             i = row*16+col;

  23:             

  24:             //divides a row of 16 into two columns of 8

  25:             if(col==8)

  26:                 printf(" ");

  27:             

  28:             //print the hex value if the current index is in range.

  29:             if(i<length)

  30:                 printf("%02X", start[i]);

  31:             //print a blank if the current index is past the end

  32:             else

  33:                 printf("  ");

  34:             

  35:             //print a space to keep the values separate

  36:             printf(" ");

  37:         }

  38:         

  39:         //create a vertial seperator between hex and ascii representations

  40:         printf(" ");

  41:  

  42:         //print ascii representation

  43:         for(col = 0; col<16; col++)

  44:         {

  45:             //calculate the current index

  46:             i = row*16+col;

  47:             

  48:             //divides a row of 16 into two coumns of 8

  49:             if(col==8)

  50:                 printf("  ");

  51:             

  52:             //print the value if it is in range

  53:             if(i<length)

  54:             {

  55:                 //print the ascii value if applicable

  56:                 if(start[i]>0x20 && start[i]<0x7F)  //A-Z

  57:                     printf("%c", start[i]);

  58:                 //print a period if the value is not printable

  59:                 else

  60:                     printf(".");

  61:             }

  62:             //nothing else to print, so break out of this for loop

  63:             else

  64:                 break;

  65:         }

  66:         

  67:         //create a new row

  68:         printf("\n");

  69:     }

  70: }

  71:  

  72: // Prints the contents of memory in hex and ascii.

  73: // Prints the memory between and including the

  74: // two "end1" and "end2" pointers.

  75: void Print_Memory(const unsigned char * end1, const unsigned char * end2)

  76: {

  77:     if(end2 >= end1)

  78:         Print_Memory(end1, end2 - end1 + 1);

  79:     else

  80:         Print_Memory(end2, end1 - end2 + 1);

  81: }

  82:  

  83: int main(int argc, char **args)

  84: {

  85:     const char start [] = "hi there!  You're looking at me in memory!";

  86:     const char * end = start + (int)strlen(start);

  87:  

  88:     Print_Memory((unsigned char *)start, (unsigned char *)end);

  89:  

  90:     return 0;

  91: }

Continue reading

Code is Poetry

I recently ordered a new mug from zazzle.com.  “Code is poetry,” it says on one side, with my favorite programming language, C♯, on the other.  Code really is poetry, and now let me relate a memory that I thought of on my drive home from work today.  (Sorry in advance.  This post doesn’t have very much focus.)
code_is_poetry_mug C#_mug

Continue reading

RoomieScript demo

I gave a presentation for my ENGE 1104 (Exploration of a Digital Future) class about home automation.  My team members talked about X10, Z-Wave, ControlThink’s SDK, and some project ideas.  I gave a brief overview of a project that I have been working on sporadically for a couple years, Roomie.  In doing so, I for created some documentation, so I figured I’d publish some of it here.  Here is an example of RoomieScript and it’s execution:
RoomieScript sample

FRI Version 1.5.0.0 Source Code

Visual C# Express

I have a friend who is an open-source fiend, and so he nag nag nagged me to make the source code  for FRI available for download.  After a lot of re-organizing and commenting, here it is!  The "Frie" project in the Visual Studio solution is the FRI back-end, stands for "Fantastic ROM Indexer Engine".  Yes, it is pronounced "free" just like the front-end. :-)

I’m putting this code out on the Internet, but please keep in mind that FRI is still my baby.  If you want to make a derivative work, email me first for permission.

Download FRI Version 1.5.0.0 Source Code here
Creative Commons License

FRI Version 1.4.0.0

 image
Download Here

Change Log:

  • Fix: emulator compatibility is now caps-insensitive
  • Fix: emulator selector centered and has correct icon
  • Re-Write: Emulator loader re-written.  The emulator selector now looks finished.  Note that ROMs can no longer be opened in bulk.
  • New Feature: Image Health (good dump, bad dump) can now be searched.
  • New Feature: The default size of the Advanced Search window is now just the right size to fit all of the content.
    fri version 1.4.0.0 advanced search window
  • New Feature: The default size of the Emulator Selector window is not just the right size to fit all of the content.
    Here is the Emulator Selector when I select a PlayStation ROM to play. (I don’t have any PlayStation emulator configured to work with FRI, so it just shows all of the emulators.)
    fri version 1.4.0.0 emulator selector
    Here is the Emulator Selector when I select a Game Boy ROM to play.  Since I have more than one Game Boy-compatible emulator configured in FRI, it gives me a choice.
    fri version 1.4.0.0 emulator selector 
  • New Feature:  More-detailed status messages in the status bar.
    fri version 1.4.0.0 status bar (split picture)
  • Tweak:  Many improvements to program responsiveness.  Previous versions of FRI freeze temporarily when scanning for a large number of new ROMs, or when populating the ROM list when a large number of ROMs.  Various tweaks, combined with the new status bar messages, ensure that FRI remains responsive and keep the user in control.
  • New Feature: Maximum number of players can now be an implied meaning of a File Tag.  This means that if a ROM has "[p1]" in the file name, then FRI will recognize it as a 1-player game.  As always, the user can create new tags, so if the user has a lot of ROMs with "(1 player)" denoting that it is a 1-player ROM, then he can easily add that to the File Flag Vocabulary.
    fri version 1.4.0.0 File Flag Vocabulary
  • New Feature:  FRI now has the ability to randomly-select a ROM in the ROM-list, or (if multiple ROMs are already selected) to randomly-select a ROM out of the selected ROMs.
    fri version 1.4.0.0 select random feature
  • New Feature: Now when FRI is closed it only saves the database if changes have been made.
    fri version 1.4.0.0 smarter saving