Converting from Windows 1252 to UTF8 in Java: null characters with CharsetDecoder/Encoder

后端 未结 2 2297
执念已碎
执念已碎 2021-02-20 05:53

I know it\'s a very general question but I\'m becoming mad.

I used this code:

String ucs2Content = new String(bufferToConvert, inputEncoding);        
          


        
2条回答
  •  死守一世寂寞
    2021-02-20 06:25

    I am not sure how you get a sequence of null characters. Try this

    String outputEncoding = "UTF-8";
    Charset charsetOutput = Charset.forName(outputEncoding);
    CharsetEncoder encoder = charsetOutput.newEncoder();
    
    // Convert the byte array from starting inputEncoding into UCS2
    byte[] bufferToConvert = "Hello World! £€".getBytes();
    CharBuffer cbuf = decoder.decode(ByteBuffer.wrap(bufferToConvert));
    
    // Convert the internal UCS2 representation into outputEncoding
    ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(cbuf));
    System.out.println(new String(bbuf.array(), 0, bbuf.limit(), charsetOutput));
    

    prints

    Hello World! £€
    

提交回复
热议问题