title: Converting a C-ctyle string encoded as a character array to a Java String
body: <p>I have a C-style string encoded as an array of characters in Java, but I would like to convert this array to a Java String. I tried using the matching constructor call,</p>
  
  <pre><code>String toRet = new String(new char[]{'B','A','D','\0', 'G', 'A', 'R', 'B', 'A', 'G', 'E'});
  System.out.println(Arrays.toString(toRet.toCharArray()));
  </code></pre>
  
  <p>But the result is incorrect, and in fact oddly buggy. Here's what the above code outputs:</p>
  
  <pre><code>[B, A, D,
  </code></pre>
  
  <p>And here's what I want </p>
  
  <pre><code>[B, A, D]
  </code></pre>
  
  <p>I'm running on openJdk6 on Ubuntu. I haven't tested the above code on other VM's.</p>