Search code examples
androidurlunicodeencode

How to convert string into unicode/utf for passing in a url?


My string is:

ĐT70, Châu Linh, Tùng Ảnh, Đức Thọ, Hà Tĩnh, Vietnam.

While passing this text in a url query string, it is coming as:

ĐT70%2C+Châu+Linh%2C+Tùng+Ảnh%2C+Đức+Thọ%2C+Hà+Tĩnh%2C+Vietnam.

But I want it in this format:

\U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129nh, Vietnam.

How could I get that string in this format? Please answer.


Solution

  • After struggling finally i found solution.

     //String that you want to convert
      String mystring = "133 Phùng Hưng, Cửa Đông, Hoàn Kiếm, Hà Nội, Vietnam";
    
        //unicodeString is the expected output in unicode 
        String unicodeString = getUnicodeString(escapeUnicodeText(mystring));
    
       //i want to make small u into Capital from unicode String
      String resultUnicode = unicodeString.replace("\\u", "\\U");
    
        try {
            String text = URLEncoder.encode(mystring, "UTF-8");
            Log.v("currentDateTimeString", "text String:" + text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    Create method escapeUnicodeText to get your Unicode Text into unicode fromat for example if you pass input string ♫ é in escapeUnicodeText() you will get in unicode output like \u266b \u00e9

    public String escapeUnicodeText(String input) {
    
        StringBuilder b = new StringBuilder(input.length());
    
        java.util.Formatter f = new java.util.Formatter(b);
    
        for (char c : input.toCharArray()) {
            if (c < 128) {
                b.append(c);
            } else {
                f.format("\\u%04x", (int) c);
            }
        }
    
        return b.toString();
    }
    

    Pass output of escapeUnicodeText into getUnicodeString() you will get your expected result like \U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129nh, Vietnam.

     public String getUnicodeString(String myString) {
        String text = "";
        try {
    
             byte[] utf8Bytes = myString.getBytes("UTF8");
            text = new String(utf8Bytes, "UTF8");
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return text;
    }