title: How to convert a hex char to binary in C++?
body: <p>I am trying to convert two (2) inputs from the user.
  I am taking input in variable <code>M</code>. The <code>K</code> variable is unused.</p>
  
  <p>I tried to solve it using <code>for</code> loop, but, I could not do it. The first loop could not stop.</p>
  
  <p>Is it wrong to use an array?
  And, can I use <code>for</code> loop after to input the binary output into a new array?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>include&lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      char M[64],K[64];
      long int i=0;
  
      cout&lt;&lt;"Enter M: ";
      cin&gt;&gt;M;
      cout&lt;&lt;"M= ";
  
      while(M[i])
      {
          switch(M[i])
          {
          case '0':
              cout&lt;&lt;"0000";
              break;
          case '1':
              cout&lt;&lt;"0001";
              break;
          case '2':
              cout&lt;&lt;"0010";
              break;
          case '3':
              cout&lt;&lt;"0011";
              break;
          case '4':
              cout&lt;&lt;"0100";
              break;
          case '5':
              cout&lt;&lt;"0101";
              break;
          case '6':
              cout&lt;&lt;"0110";
              break;
          case '7':
              cout&lt;&lt;"0111";
              break;
          case '8':
              cout&lt;&lt;"1000";
              break;
          case '9':
              cout&lt;&lt;"1001";
              break;
          case 'A':
              cout&lt;&lt;"1010";
              break;
          case 'B':
              cout&lt;&lt;"1011";
              break;
          case 'C':
              cout&lt;&lt;"1100";
              break;
          case 'D':
              cout&lt;&lt;"1101";
              break;
          case 'E':
              cout&lt;&lt;"1110";
              break;
          case 'F':
              cout&lt;&lt;"1111";
              break;
          case 'a':
              cout&lt;&lt;"1010";
              break;
          case 'b':
              cout&lt;&lt;"1011";
              break;
          case 'c':
              cout&lt;&lt;"1100";
              break;
          case 'd':
              cout&lt;&lt;"1101";
              break;
          case 'e':
              cout&lt;&lt;"1110";
              break;
          case 'f':
              cout&lt;&lt;"1111";
              break;
          default:
              cout&lt;&lt;"nInvalid hexadecimal digit "&lt;&lt;M[i];
          }
          i++;
      }
      return 0;
      cout&lt;&lt;"\nEnter K: ";
      cin&gt;&gt;K;
      cout&lt;&lt;"K: ";
      while(K[i])
      {
          switch(K[i])
          {
          case '0':
              cout&lt;&lt;"0000";
              break;
          case '1':
              cout&lt;&lt;"0001";
              break;
          case '2':
              cout&lt;&lt;"0010";
              break;
          case '3':
              cout&lt;&lt;"0011";
              break;
          case '4':
              cout&lt;&lt;"0100";
              break;
          case '5':
              cout&lt;&lt;"0101";
              break;
          case '6':
              cout&lt;&lt;"0110";
              break;
          case '7':
              cout&lt;&lt;"0111";
              break;
          case '8':
              cout&lt;&lt;"1000";
              break;
          case '9':
              cout&lt;&lt;"1001";
              break;
          case 'A':
              cout&lt;&lt;"1010";
              break;
          case 'B':
              cout&lt;&lt;"1011";
              break;
          case 'C':
              cout&lt;&lt;"1100";
              break;
          case 'D':
              cout&lt;&lt;"1101";
              break;
          case 'E':
              cout&lt;&lt;"1110";
              break;
          case 'F':
              cout&lt;&lt;"1111";
              break;
          case 'a':
              cout&lt;&lt;"1010";
              break;
          case 'b':
              cout&lt;&lt;"1011";
              break;
          case 'c':
              cout&lt;&lt;"1100";
              break;
          case 'd':
              cout&lt;&lt;"1101";
              break;
          case 'e':
              cout&lt;&lt;"1110";
              break;
          case 'f':
              cout&lt;&lt;"1111";
              break;
          default:
              cout&lt;&lt;"nInvalid hexadecimal digit "&lt;&lt;K[i];
          }
          i++;
      }
      return 0;
  }
  </code></pre>