title: How to convert hex value to binary value in C
body: <p>Can someone please help me fix my code.I have a text file with hexadecimal values. Now I need to convert the hexadecimal value to binary and print it. This is my code so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;stdlib.h&gt; 
  #define MAX 1000 
  
  int hex_to_binary(char *argv[]) 
  
  int main(int argc,char *argv[]) 
  { 
      FILE *file; 
      file = fopen(argv[1],"r"); 
      char line[100]; 
  
      while(!feof(file)){ 
          fgets(line,100,file); 
          hex_to_binary(line); 
      } 
      fclose(file); 
  
      return 0; 
  } 
  
  int hex_to_binary(char *argv[]){ 
      char binaryNumber[MAX],hexaDecimal[MAX]; 
      long int i=0; 
  
      scanf(“%s”,argv[1]); 
  
      printf("\nEquivalent binary value: "); 
      while(hexaDecimal[i]){ 
           switch(hexaDecimal[i]){ 
               case '0': printf("0000"); break; 
               case '1': printf("0001"); break; 
               case '2': printf("0010"); break; 
               case '3': printf("0011"); break; 
               case '4': printf("0100"); break; 
               case '5': printf("0101"); break; 
               case '6': printf("0110"); break; 
               case '7': printf("0111"); break; 
               case '8': printf("1000"); break; 
               case '9': printf("1001"); break; 
               case 'A': printf("1010"); break; 
               case 'B': printf("1011"); break; 
               case 'C': printf("1100"); break; 
               case 'D': printf("1101"); break; 
               case 'E': printf("1110"); break; 
               case 'F': printf("1111"); break; 
               case 'a': printf("1010"); break; 
               case 'b': printf("1011"); break; 
               case 'c': printf("1100"); break; 
               case 'd': printf("1101"); break; 
               case 'e': printf("1110"); break; 
               case 'f': printf("1111"); break; 
               default:  printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]);                              return 0; 
       } 
       i++; 
  } 
  
  return 0; 
  </code></pre>
  
  <p>} </p>
  
  <p>I keep getting errors such as:</p>
  
  <pre><code>part1_V2.c: In function ‘hex_to_binary’:
  part1_V2.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
  part1_V2.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’    token
  part1_V2.c:26: error: stray ‘\342’ in program
  part1_V2.c:26: error: stray ‘\200’ in program
  part1_V2.c:26: error: stray ‘\234’ in program
  part1_V2.c:26: error: stray ‘\342’ in program
  part1_V2.c:26: error: stray ‘\200’ in program
  part1_V2.c:26: error: stray ‘\235’ in program
  part1_V2.c:59: error: expected ‘{’ at end of input
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p>Ok guys thanks for your help I got my code working but now I'm have trouble with the output:</p>
  
  <p>The textfile I pass to the main function contains: </p>
  
  <p>"1283" (line1)
     "5105" (next line)</p>
  
  <p>These are the hex values on the file. So when I run the program I get output:</p>
  
  <p>Equivalent binary value: 0001001010000011
  nvalid hexadecimal digit 
  Invalid hexadecimal digit<br>
  Equivalent binary value: 0101000100000101                            </p>
  
  <p>I'm wondering why I'm getting the invalid hexadecimal digit output? Is it because it is trying to convert "\n" or empty space to binary?</p>