title: decoding and encoding strings, ISO-8859-1 to UTF-8 in Java
body: <p>I have read the other posts on this issue, but the solutions they presented did not work for me. Actually, the official Java documentation also did not work as intended (I am using Java 11) : <a href="https://docs.oracle.com/javase/tutorial/i18n/text/string.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/tutorial/i18n/text/string.html</a></p>
  <p>My problem is that I am reading one byte at a time from a byte buffer, putting that in a byte array, and making a String out of that byte array. The bytes I read are from an embedded system that can only send ISO-8859-1 bytes, so I end up with a byte array with ISO-8859-1 bytes and the Java String I end up getting is thus ISO-8859-1 encoded. No problem here. The String in IntelliJ looks like this :</p>
  <p><a href="https://i.stack.imgur.com/qyGL7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qyGL7.png" alt="enter image description here" /></a></p>
  <p>The bytes I am trying to convert from ISO-8859-1 to UTF-8 are the ones in yellow. I want them to be UTF-8, so in the end the &quot;C9&quot; byte should be replace by the &quot;C3A9&quot; bytes.</p>
  <p><strong>The first step works correctly</strong>, I do this : <code>maintenanceResponseString.getBytes(StandardCharsets.UTF_8)</code> and I get the right bytes that I want, the UTF-8 encoding of the string, that's good :</p>
  <p><a href="https://i.stack.imgur.com/BJFNZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BJFNZ.png" alt="enter image description here" /></a></p>
  <p><strong>The problem comes in here</strong> , when I try to make a STRING out of these new (and GOOD) bytes, like this :</p>
  <pre><code>new String(maintenanceResponseString.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/QtUQh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QtUQh.png" alt="enter image description here" /></a></p>
  <p>The <strong>old bytes are back</strong> ?!! It's like the &quot;getBytes(UTF-8)&quot; never actually happened. That is NOT what the documentation says should happen... what am I missing here ? I have done tests and the string really is still ISO-8859-1 encoded... I don't know what is going on here. Where are the bytes from &quot;getBytes&quot; ?</p>
  <p>How do you convert a String that contains ISO-8859-1 bytes to UTF-8 bytes ? I'm out of alternatives and I need to get it done real bad for a pro project... this should be easy !</p>
  <p>Note : I have tried alternatives like</p>
  <pre class="lang-java prettyprint-override"><code>ByteBuffer buffer = StandardCharsets.UTF_8.encode(s);
  return StandardCharsets.UTF_8.decode(buffer).toString();
  </code></pre>
  <p>But the exact same thing happens.</p>
  <p>Thank you in advance for your help.</p>
  <p><em><strong>EDIT :</strong></em>
  With some info in the comments about how Strings in Java 9+ get represented internally not as UTF-16 only anymore, but Latin-1 (why...), I think that is what made me think the Strings were &quot;internally encoded in Latin-1&quot; when it is just the default representation of the String <strong>if we don't specify the encoding we want to use when displaying the String</strong>.</p>
  <p>From what I undestand now the String itself is <strong>not bound to any encoding</strong>, and you can CHOOSE the encoding you want to display it in when it gets written.
  Actually my issue is that the String ends up written to an XML file via JAXB marshalling in LATIN-1, and I now think the issues lies over there... I will dig further when I access my work computer again and report here</p>