user21399
user21399

Reputation: 13

Convert from URL encoding to UTF-8

I'm trying to convert from a URL-encoded ASCII byte array to UTF-8 in Java.

The input ASCII string Fa%C3%A7ade should be converted to the output UTF-8 string Façade.

Upvotes: 0

Views: 3677

Answers (3)

icza
icza

Reputation: 417422

Your input string is not ASCII but a URL-encoded string.

You can decode it like this:

String s = "Fa%C3%A7ade";
System.out.println(URLDecoder.decode(s, "UTF-8"));

A String in Java is represented as a character array (char[]), it is not encoded in any encoding. Encoding comes into play when a String is converted to a byte array or a byte array is converted to a String.

So if you have a byte array which is a String encoded using UTF-8 encoding, you can convert it to a String like this:

byte[] arr = {104, 101, 108, 108, 111};
String s = new String(arr, StandardCharsets.UTF_8);
System.out.println(s); // Prints "hello"

// Or your input string would be:
arr = {70, 97, -61, -89, 97, 100, 101};
s = new String(arr, StandardCharsets.UTF_8);
System.out.println(s); // Prints "Façade"

Upvotes: 1

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Using Apache commons-codec

byte[] bytes = StringUtils.getBytesUtf8(asciiString);
String utfString = new String(bytes);

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44813

How about

new String (bytes, "UTF-8");

Upvotes: 0

Related Questions