title: How to decode/encode a UTF-8 char in c++ without wchar_t
body: <p>As the title states, I am attempting to decode/encode UTF-8 characters to a char, but I want to do it without using wchar_t or the like. I want to do the leg work myself. This way I know that I understand it, which I obviously don't or it would be working. I've spent about a week toying with it and am just not making progress. </p>
  
  <p>I have tried several ways, yet always seem to produce incorrect results. My latest attempt:</p>
  
  <pre><code>ifstream ifs(FILENAME);
      if(!ifs) {
          cerr &lt;&lt; "Open: " &lt;&lt; FILENAME &lt;&lt; "\n";
          exit(1);
      }
  
      char in;
  
      while (ifs &gt;&gt; std::noskipws &gt;&gt; in) {
          int sz = 1;
          if ((in &amp; 0xc0) == 0xc0) //0xc0 = 0b11000000
          {
                  sz++;
                  if((in &amp; 0xE0) == 0xE0) //0xE0 = 0b11100000
                  {
                      sz++;   
                      if((in &amp; 0xF0) == 0xF0) //0xF0 = 0b11110000
                          sz++;   
                  }
          }
          cout &lt;&lt; sz &lt;&lt; endl;
  
  unsigned int a = in;
      for(int i = 1; i &lt; sz; i++) {
          ifs &gt;&gt; in;
          a += in;
      }
  </code></pre>
  
  <p>Why do this code not work? I simply do not understand.  </p>
  
  <p>EDIT: Copy+Paste spaghetti...two different var names </p>