0

How can I make String Стек look like %D0%A1%D1%82%D0%B5%D0%BA? Which encoding is it? How can I do it with Java? I thought it's UTF-8:

 String myString = "Стек";
 byte text[] = myString.getBytes();
 String value = new String(text, "UTF-8");
 System.out.println(value);

But no, I've got Стек in output.

1
  • 2
    Looks like URL encoding to me. Note that UTF-8 is a text->binary conversion. You're looking for a text->text conversion. Commented Jan 31, 2014 at 13:24

3 Answers 3

2

It's not UTF-8, it's URL-like encoding, and you can get it using the URLEncoder class:

String encoded = URLEncoder.encode("Стек");
System.out.println(encoded);

Result:

%D0%A1%D1%82%D0%B5%D0%BA

IDEOne working example

Sign up to request clarification or add additional context in comments.

Comments

2

The text that you've shown is Percent encoded or URL encoded.

You can use URLEncoder for converting it to the desired format:

String value = URLEncoder.encode("Стек", "ISO-8859-1");

Comments

2

You can use the URLEncoder class to convert a String to percent encoding:

import java.net.URLEncoder;

System.out.println(URLEncoder.encode("Стек", "utf-8"));

You'll also need to catch UnsupportedEncodingException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.