kukis
kukis

Reputation: 4644

Another string conversion URL utf8 java

I've got String in java like this

%C5%82%C3%B3

And I want to convert it to UTF-8. The output string should looks like this

łó

How can I achieve that? I went through other similiar topics on SO but nothing helps. I've tried:

        byte[] b = myString.getBytes();
        byte[] b1 = myString.getBytes(Charset.defaultCharset());
        byte[] b2 = myString.getBytes(Charset.forName("UTF-8"));
        byte[] b3 = myString.getBytes(Charset.forName("ISO-8859-2"));
        byte[] b4 = myString.getBytes(Charset.forName("ISO-8859-1"));
        System.out.println(new String(b, "UTF-8"));
        System.out.println(new String(b, "ISO-8859-1"));
        System.out.println(new String(b1, "UTF-8"));
        System.out.println(new String(b1, "ISO-8859-1"));
        System.out.println(new String(b2, "UTF-8"));
        System.out.println(new String(b2, "ISO-8859-1"));
        System.out.println(new String(b3, "UTF-8"));
        System.out.println(new String(b3, "ISO-8859-1"));
        System.out.println(new String(b4, "UTF-8"));
        System.out.println(new String(b4, "ISO-8859-1"));

In above code myString contains

%C5%82%C3%B3

and System.out always prints

%C5%82%C3%B3

Any ideas or should I start writing my own conversion method?

Upvotes: 0

Views: 220

Answers (2)

1218985
1218985

Reputation: 8032

You can use URLDecoder class to decode your encoded String:

System.out.println(URLDecoder.decode("%C5%82%C3%B3", "UTF-8"));

Upvotes: 0

peter.petrov
peter.petrov

Reputation: 39477

This String is URL-encoded. You need to decode it.

Make some tests here.

http://meyerweb.com/eric/tools/dencoder/

Then try this.

String result = URLDecoder.decode("%C5%82%C3%B3", "UTF-8");

See also: http://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html

Upvotes: 3

Related Questions