/* Lecture code 1.1
 *
 * Code to read in the contents of a file and print out each character using its hexadecimal
 * representation, rather than its character equivalent.
 */

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

/* Constant defining the number of characters to print out on each line. */
const int CHARS_PER_LINE = 16;
int main()
{
	ifstream input;
	
	/* Open the file in binary mode.  If it fails, abort with an error.
	 * This will look for "logo.gif" in the same directory that the program
	 * is running in.
	 */
	input.open("logo.gif", ios::in | ios::binary);
	if(!input.is_open())
	{
		cerr << "That file doesn't exist." << endl;
		return 0;
	}

	/* Loop until we read in all characters. */
	while(!input.eof())
	{
		/* Read in enough characters for a line.  This might abort early. */
		for(int i = 0; i < CHARS_PER_LINE; i++)
		{
			/* Read in as an unsigned char, to avoid errors with positive/negative
			 * ASCII values.
			 */
			unsigned char ch;
			
			/* Use noskipws to read in the next character, not just the next non-whitespace
			 * character.
			 */
			input >> noskipws >> ch;

			/* Check for end-of-file, abort if we're done. */
			if(input.eof())
				break;

			/* Set up cout to:
			 * 1. Print in hexadecimal
			 * 2. Make the next operation take up at least two characters width.
			 * 3. Pad extra space with the character 0
			 * 4. Print all characters in uppercase
			 *
			 * Then, typecast the character to an int (using static_cast) and print
			 * it out.
			 */
			cout << hex << setw(2) << setfill('0') << uppercase << static_cast<int>(ch) << ' ';
		}

		cout << endl;
	}
	return 0;
}