title: utf-8 decoding in java
body: <p>I'm trying to pass parameters from a PHP middle tier to a java backend that understands J2EE. I'm writing the controller code in Groovy. In there, I'm trying to decode some parameter that will likely contain international characters.</p>
  
  <p>I am really puzzled by the results of my debugging this problem so far, hence I wanted to share it with you in the hope that someone will be able to give the correct interpretation of my results.</p>
  
  <p>For the sake of my little test, the parameter I'm passing is "déjeuner". Just to be sure, System.out.println("déjeuner") correctly gives me:</p>
  
  <pre><code>déjeuner
  </code></pre>
  
  <p>in the console</p>
  
  <p>Now following are the char/dec and hex values of each char of the original string:</p>
  
  <pre><code>next char: d 100 64
  next char: ? -61 c3
  next char: ? -87 a9
  next char: j 106 6a
  next char: e 101 65
  next char: u 117 75
  next char: n 110 6e
  next char: e 101 65
  next char: r 114 72
  </code></pre>
  
  <p>note that the c3a9 sequence in UTF-8 is the wished-for character: <a href="http://www.fileformat.info/info/unicode/char/00e9/index.htm" rel="noreferrer">http://www.fileformat.info/info/unicode/char/00e9/index.htm</a></p>
  
  <p>Now if I try to read this string as an UTF-8 string, as in stmt.getBytes("UTF-8"), I suddenly end up having a 11 bytes sequence, as follows:</p>
  
  <pre><code>64 c3 83 c2 a9 6a 65 75 6e 65 72
  </code></pre>
  
  <p>whereas stmt.getBytes("iso-8859-1") gives me 9 bytes:</p>
  
  <pre><code>64 c3 a9 6a 65 75 6e 65 72
  </code></pre>
  
  <p>note the c3a9 sequence here!</p>
  
  <p>now if I try to convert the UTF-8 sequence to UTF-8, as in </p>
  
  <pre><code>new String(stmt.getBytes("UTF-8"), "UTF-8");
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>next char: d 100 64
  next char: ? -61 c3
  next char: ? -87 a9
  next char: j 106 6a
  next char: e 101 65
  next char: u 117 75
  next char: n 110 6e
  next char: e 101 65
  next char: r 114 72
  </code></pre>
  
  <p>note the c3a9 sequence</p>
  
  <p>while</p>
  
  <pre><code>new String(stmt.getBytes("iso-8859-1"), "UTF-8")
  </code></pre>
  
  <p>results in:</p>
  
  <pre><code>next char: d 100 64
  next char: ? -23 e9
  next char: j 106 6a
  next char: e 101 65
  next char: u 117 75
  next char: n 110 6e
  next char: e 101 65
  next char: r 114 72
  </code></pre>
  
  <p>note the e9 which in utf-8 (and ascii) is, again, the 'é' character that I'm longing for.</p>
  
  <p>Unfortunately, in neither case am I ending up with a proper string that would display like the literal string "déjeuner". Strangely enough, the byte sequences both seem correct though.</p>