title: Java bug? Why extra zero byte in utf8 encoding?
body: <p>The following code </p>
  
  <pre><code>public class CharsetProblem {
  public static void main(String[] args) {
      //String str = "aaaaaaaaa";
      String str = "aaaaaaaaaa";
      Charset cs1 = Charset.forName("ASCII");
      Charset cs2 = Charset.forName("utf8");
  
      System.out.println(toHex(cs1.encode(str).array()));
      System.out.println(toHex(cs2.encode(str).array()));
  
  }
  
  public static String toHex(byte[] outputBytes) {
  
      StringBuilder builder = new StringBuilder();
  
      for(int i=0; i&lt;outputBytes.length; ++i) {
          builder.append(String.format("%02x", outputBytes[i]));
      }
  
      return builder.toString();
  }
  }
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>61616161616161616161
  6161616161616161616100
  </code></pre>
  
  <p>i.e. utf8 encoding returns excess byte. If we take less a-s, then we'll have no excess bytes. If we take more a-s we can get more and more excess bytes.</p>
  
  <p>Why?</p>
  
  <p>How one can workaround this?</p>