title: Hex file reading in C programming?
body: <p>I had a question:</p>
  
  <p>I am writing a c program that functions much like a hex editor. I want to take in a file and view its hexadecimal values. For example, say I had the text file "helloworld.txt" that simply had the words "Hello World!" in it, I want to specify to the program to take that file, read its hexidecimal values (again the file is a simple .txt file and not a binary file) and store it in an array for later. </p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
     char ch, file_name[25];
     FILE *fp;
  
     printf("Enter the name of file you wish to see\n");
     gets(file_name);
  
     fp = fopen(file_name,"rb"); // read binary mode
  
     if( fp == NULL ) //error checking
     {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
     }
  
     printf("The contents of %s file are :\n", file_name);
  
     while( ( ch = fgetc(fp) ) != EOF )
        printf("%c",ch);
  
     fclose(fp);
     return 0;
  }
  </code></pre>
  
  <p>As you can see this is really just a simple file in, read and print contents. However, the problem is that even though I'm in "read binary" mode, it won't give me the hexadecimal values of the text file. </p>
  
  <blockquote>
    <p>How can I structure this program so that it reads the hexadecimal values of a standard text file and either (a) puts them in an array (which I think should be simple enough with a for loop) or (b) prints them out to the screen? </p>
  </blockquote>
  
  <p>I tried googling to see if others were trying to do the same thing, but people were either already reading from a binary file or already had hexadecimal values in the file. I have plain text in a regular .txt file, but I want the hexadecimal values underneath (in other words a sort of hexdump of the file). I hope my question and what I'm trying to do makes sense. If you have any questions please feel free to let me know! Thanks in advance for all your help! I really do appreciate it!</p>