title: Examining Output of raw files C++
body: <p>Hi I am reading in a binary file formatted in hex. It is an image file below is a short example of the first few lines of code using hd ... |more command on linux. The image is a binary graphic so the only pixel colours are either black or white. It is a 1024 by 1024 image however the size comes out to be 2097152 bytes</p>
  
  <p><strong>00000000</strong>  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|</p>
  
  <p><strong>000dfbf0</strong>  00 00 00 00 00 00 00 00  00 00 00 00 ff 00 ff 00  |................|</p>
  
  <p><strong>000dfc00</strong>  ff 00 ff 00 ff 00 00 00  00 00 00 00 00 00 00 00  |................|</p>
  
  <p><strong>000dfc10</strong>  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|</p>
  
  <p>This is the code I am using to read it in found in another thread on SO </p>
  
  <pre><code>ifstream file (argv[1], ios::in | ios::binary | ios::ate);
  ifstream::pos_type fileSize;
  char* fileContents;
  if(file.is_open())
  {
      fileSize = file.tellg();
      fileContents = new char[fileSize];
      file.seekg(0, ios::beg);
      if(!file.read(fileContents, fileSize))
      {
          cout &lt;&lt; "fail to read" &lt;&lt; endl;
      }
   file.close();
   cout &lt;&lt; fileSize &lt;&lt; endl;
  </code></pre>
  
  <p>The code works however when I run this for loop</p>
  
  <pre><code>for (i=0; i&lt;2097152; i++)
   printf("%hd",fileContents[i]);
  </code></pre>
  
  <p>The only thing printed out are zeros and no 1s. Why is this are my parameters in printf not correctly specifying the pixel size. I know for a fact that there are 1's in the image representing the white areas. Also how do i figure out how many bytes represent a pixel in this image.</p>