This discussion is archived
3 Replies Latest reply: Oct 29, 2008 9:42 AM by DrClap RSS

java.nio.charset enconding special characters

793982 Newbie
Currently Being Moderated
I'm trying to encode and decode a String with special characters, but the result String comes with extra chars.
What am I doing wrong?
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class CharsetTest {
    public static void main(String[] args) throws Exception {
        String s1 = "üça";
        Charset utf8 = Charset.forName("UTF-8");
        ByteBuffer bb = utf8.newEncoder().encode(CharBuffer.wrap(s1.toCharArray()));
        CharBuffer cb = utf8.newDecoder().decode(bb);
        String s2 = new String(cb.array());

        //just trying to diagnose
        System.out.println(s1+" "+s2+" "+s1.length()+" "+s2.length());
        for( char c : s2.toCharArray() )
            System.out.print((int)c+" ");
Henrique Abreu