title: Converting from Windows 1252 to UTF8 in Java: null characters with CharsetDecoder/Encoder
body: <p>I know it's a very general question but I'm becoming mad. </p>
  
  <p>I used this code:</p>
  
  <pre><code>String ucs2Content = new String(bufferToConvert, inputEncoding);        
          byte[] outputBuf = ucs2Content.getBytes(outputEncoding);        
          return outputBuf;
  </code></pre>
  
  <p>But I read that is better to use CharsetDecoder and CharsetEncoder (I have contents with some character probably outside the destination encoding). I've just written this code but that has some problems:</p>
  
  <pre><code>// Create the encoder and decoder for Win1252
  Charset charsetInput = Charset.forName(inputEncoding);
  CharsetDecoder decoder = charsetInput.newDecoder();
  
  Charset charsetOutput = Charset.forName(outputEncoding);
  CharsetEncoder encoder = charsetOutput.newEncoder();
  
  // Convert the byte array from starting inputEncoding into UCS2
  CharBuffer cbuf = decoder.decode(ByteBuffer.wrap(bufferToConvert));
  
  // Convert the internal UCS2 representation into outputEncoding
  ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(cbuf));
  return bbuf.array();
  </code></pre>
  
  <p>Indeed this code <strong>appends to the buffer a sequence of null character</strong>!!!!!</p>
  
  <p>Could someone tell me where is the problem? I'm not so skilled with encoding conversion in Java. </p>
  
  <p>Is there a better way to convert encoding in Java?</p>