Android - How to Convert String to utf-8 in android

62,630

Solution 1

In http://developer.android.com/reference/java/net/URLEncoder.html you can read that the you used is deprecated and that you should use static String encode(String s, String charsetName)

So URLEncoder.encode("臺北市", "utf-8") should do the trick.

Solution 2

public class StringFormatter {

    // convert UTF-8 to internal Java String format
    public static String convertUTF8ToString(String s) {
        String out = null;
        try {
            out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return out;
    }

    // convert internal Java String format to UTF-8
    public static String convertStringToUTF8(String s) {
        String out = null;
        try {
            out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return out;
    }

}

You can convert your string using StringFormatter class to your code.

You want to convert to UTF-8:

String normal="This normal string".
String utf=StringFormatter.convertStringToUTF8(normal);

You want to convert UTF-8 to normal format:

String normal=StringFormatter.convertUTF8ToString(normal);

Solution 3

You can just use,

URLEncoder.encode(string, "UTF-8");

This will encode your "string: in UTF-8 format.

Put it in a try/catch and check for IllegalArgumentException if you want to. And if you have any spaces in your string, please replace it with

string.replace(" ", "%20");

Solution 4

use this:

        URLEncoder.encode("臺北市", "UTF-8");
Share:
62,630
Zih-Yang Lin
Author by

Zih-Yang Lin

Updated on July 22, 2022

Comments

  • Zih-Yang Lin
    Zih-Yang Lin almost 2 years

    I can't convert a String to UTF-8 in android. please help me!!

    s1=URLEncoder.encode("臺北市")
    

    result : %EF%BF%BDO%EF%BF%BD_%EF%BF%BD%EF%BF%BD

    But "臺北市" should be encoded as "%E8%87%BA%E5%8C%97%E5%B8%82"

    • Zih-Yang Lin
      Zih-Yang Lin almost 9 years
      I use URLEncoder.encode("臺北市", "utf-8"),but not yet solution .
  • Zih-Yang Lin
    Zih-Yang Lin almost 9 years
    The result still be %EF%BF%BDO%EF%BF%BD_%EF%BF%BD%EF%BF%BD
  • Remy Lebeau
    Remy Lebeau almost 9 years
    %EF%BF%BDO%EF%BF%BD_%EF%BF%BD%EF%BF%BD is not a UTF-8 encoded sequence.
  • Remy Lebeau
    Remy Lebeau almost 9 years
    Your second example is encoding a String into bytes using Java's default encoding (which is not guaranteed to be UTF-8) and is then creating a new String interpreting the bytes as if they were UTF-8. That is not even close to what URLEncoder.encode() does (although your first example is correct for the question asked).
  • Zih-Yang Lin
    Zih-Yang Lin almost 9 years
    But "URLEncoder.encode("臺北市", "utf-8")" output is %EF%BF%BDO%EF%BF%BD_%EF%BF%BD%EF%BF%BD
  • Remy Lebeau
    Remy Lebeau almost 9 years
    If that is true, than Android's implementation is broken and should be reported to Google. In the meantime, url encoding is easy to implement manually. Use "臺北市".getBytes("utf-8") and then convert each non-ASCII-alphanumeric byte to its "%XX" hex representation.
  • Aritra Roy
    Aritra Roy almost 9 years
    Is your problem solved? Please accept the answer if it helped you. It is the way of saying "thank you" in SO.
  • hadi
    hadi over 8 years
    Extraordinary answer. specially the string.replace(" ", "%20"); part. this should be the accepted answer.
  • Mathews Sunny
    Mathews Sunny over 6 years
    Add some description about what you wrote above.
  • Paras Santoki
    Paras Santoki over 6 years
    It's using default encoding to convert string to UTF-8.