title: How to convert CharSequence to UTF-8 encoded byte array in Java?
body: <p>I am trying to convert a <code>CharSequence</code> to a UTF-8 encoded <code>byte[]</code> array.</p>
  <p>And i've been having problems with it, so i was going to ask stackoverflow for help. And i was going to write a <em>Java Fiddle</em> to do it:</p>
  <p><a href="https://www.mycompiler.io/view/3MliN0HgwDD" rel="nofollow noreferrer">https://www.mycompiler.io/view/3MliN0HgwDD</a></p>
  <p>Except the fiddle itself doesn't work:</p>
  <pre><code>import java.util.*;
  import java.lang.*;
  import java.io.*;
  import java.nio.*;
  import java.nio.charset;
  
  // The main method must be in a class named &quot;Main&quot;.
  class Main {
      public static byte[] charSequenceToUtf8(final CharSequence input)
      {
          //char[] chars = new char[input.length];
          //for (int i=0; i&lt;input.length; i++)
          //  chars[i] = input.charAt(i);
  
          CharBuffer charBuffer = CharBuffer.wrap(input);
          checkEquals(10, charBuffer.length(), &quot;Charbuffer is wrong length&quot;);
  
          Charset cs = Charset.forName(&quot;UTF-8&quot;); 
          ByteBuffer byteBuffer = cs.encode(charBuffer);
          checkEquals(10, byteBuffer.length(), &quot;byteBuffer is wrong length&quot;);
          
          byte[] utf8 = byteBuffer.array();        
          checkEquals(10, utf8.length, &quot;utf8 bytes is wrong length&quot;);
      }
      
      public static void checkEquals(int expected, int actual, String message)
      {
          if (expected == actual)
              return;
              
          String sExpected = String.valueOf(expected);
          String sActual = String.valueOf(actual);
          
          throw new Exception(&quot;Test failed. Expected &quot;+sExpected+&quot;, Actual &quot;+sActual+&quot;. &quot;+message);
      }
      
      public static void main(String[] args) {
          test(&quot;AAAAAAAAAA&quot;); //ten A's
      }
  }
  </code></pre>
  <p>It seems that <code>java.nio</code> requires at least Java 7 <sup><a href="https://stackoverflow.com/questions/20029271/cannot-find-package-java-nio-file">ref</a></sup>. Which is why it is confusing to me that it doesn't work in Java 16:</p>
  <p><a href="https://i.stack.imgur.com/ClxWd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ClxWd.png" alt="enter image description here" /></a></p>
  <p>So this bring up a lot of questions:</p>
  <ul>
  <li>how can i convert a <code>CharSequence</code> to a <code>byte[]</code> array? <sup><a href="https://stackoverflow.com/questions/18514836/how-to-encode-a-charsequence-using-a-charset-without-converting-to-string">1</a></sup></li>
  <li>why does it not work in Java 16?</li>
  </ul>
  <p>In the end, the actual bug is that trying to encode the string <code>AAAAAAAAA</code> returns an 11-element array:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>CharSequence</th>
  <th>UTF-8 byte array</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td><em>&quot;AA&quot;</em></td>
  <td><code>[65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAA&quot;</em></td>
  <td><code>[65, 65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAAA&quot;</em></td>
  <td><code>[65, 65, 65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAAAA&quot;</em></td>
  <td><code>[65, 65, 65, 65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAAAAA&quot;</em></td>
  <td><code>[65, 65, 65, 65, 65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAAAAAA&quot;</em></td>
  <td><code>[65, 65, 65, 65, 65, 65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAAAAAAA&quot;</em></td>
  <td><code>[65, 65, 65, 65, 65, 65, 65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAAAAAAAA&quot;</em></td>
  <td><code>[65, 65, 65, 65, 65, 65, 65, 65, 65]</code></td>
  </tr>
  <tr>
  <td><em>&quot;AAAAAAAAAA&quot;</em></td>
  <td><code>[65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 0]</code></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Why is the above code, that i stole from the linked question, failing of a string of 10 characters?</p>