title: Java - Why String( a).getBytes() == a not give the same result?
body: <p>For an example array:</p>
  
  <pre><code>byte[] arr = {37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -30, -29, -49, -45, -121, -104 };
  </code></pre>
  
  <p>And the code:</p>
  
  <pre><code>String a = new String(arr, Charset.forName("US-ASCII"));
  System.out.println(Arrays.toString(arr));
  System.out.println(Arrays.toString(a.getBytes(Charset.forName("US-ASCII"))));
  System.out.println( Arrays.equals(arr, a.getBytes(Charset.forName("US-ASCII"))) );
  </code></pre>
  
  <p>The result is:</p>
  
  <ul>
  <li><p>In "Windows-1251":</p>
  
  <pre><code>[37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -30, -29, -49, -45, -121, -104]
  [37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -30, -29, -49, -45, -121, 63]
  false
  </code></pre></li>
  <li><p>In "US-ASCII":</p>
  
  <pre><code>[37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -30, -29, -49, -45, -121, -104]
  [37, 80, 68, 70, 45, 49, 46, 53, 13, 37, 63, 63, 63, 63, 63, 63]
  false
  </code></pre></li>
  <li><p>In "UTF-8":</p>
  
  <pre><code>[37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -30, -29, -49, -45, -121, -104]
  [37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -17, -65, -67, -17, -65, -67, -17, -65, -67, -45, -121, -17, -65, -67]
  false
  </code></pre></li>
  </ul>
  
  <p>I have test with various test case and found that they give different arrays when there are negative number. And I tried with "Windows-1251" like in this <a href="https://stackoverflow.com/questions/19036870/system-text-encoding-utf8-getbytes-extra-byte">question</a> but the arrays still different.
  So my question is:</p>
  
  <ul>
  <li>Why?</li>
  <li>How to fix it?</li>
  </ul>
  
  <p>Addtional Info:</p>
  
  <ul>
  <li>I'm using JRE8 and on Windows 8.1.</li>
  </ul>
  
  <p>Resolution: Use Charset ISO-8859-1, thank to SLaks for explaining and JB Nizet for point out the ISO-8859-1</p>
  
  <pre><code>String a = new String(arr, Charset.forName("ISO-8859-1"));
  System.out.println(Arrays.toString(arr));
  System.out.println(Arrays.toString(a.getBytes(Charset.forName("ISO-8859-1"))));
  System.out.println( Arrays.equals(arr, a.getBytes(Charset.forName("ISO-8859-1"))) );
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>[37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -30, -29, -49, -45, -121, -104]
  [37, 80, 68, 70, 45, 49, 46, 53, 13, 37, -30, -29, -49, -45, -121, -104]
  true
  </code></pre>