2017-08-16 45 views
1

我試圖在我的EditText字段中使用表情符號。但問題是,當我嘗試將表情符號轉換爲hexa時,該值不正確。從Android中的editText編碼和解碼錶情符號

我用這個函數在Hexa中獲取表情符值。

public static String escapeJavaString(String st){ 
    StringBuilder builder = new StringBuilder(); 
    try { 
     for (int i = 0; i < st.length(); i++) { 
      char c = st.charAt(i); 
      if(!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)&& !Character.isWhitespace(c)){ 
       String hexa= String.valueOf(c); 
       int code = (int)c; 
       if(!(code >= 0 && code <= 255)){ 
        hexa= "0x"+Integer.toHexString(c); 
       } 
       builder.append(unicode); 
      } 
      else{ 
       builder.append(c); 
      } 
     } 
     Log.i(TAG, builder.toString()); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return builder.toString(); 
} 

我嘗試添加笑臉,它給我的價值0xd83d這是不是在下面的功能工作。

這裏是函數將Hexa值轉換爲表情符號,這個函數工作正常我試着提供手動的Hexa值。

 // 0xd83d this value is get from above function but not work 
     // in this function 
     //String hexaCode = "0xd83d"; // not work 
     String hexaCode = "0x1F60A"; // work 
     int emoji= Integer.parseInt(hexaCode.substring(2), 16); 
     reurn new String(Character.toChars(emoji)); 

您能否讓我知道我做錯了什麼。請不要引用我任何表情符號庫。

+0

我以爲你在問圖書館,請參考這裏的討論https://stackoverflow.com/questions/45453202/how-to-use-the-imagestored-image-of-device-with-text-on -textview,機器人 –

回答

2

我解決了它,這對我來說工作得很好。
這裏是代碼。

public String escapeJavaString(String st){ 
    int ss1 = 0,ss2; 
    Boolean high = true; 
    StringBuilder builder = new StringBuilder(); 
    try { 
     for (int i = 0; i < st.length(); i++) { 
      char c = st.charAt(i); 
      if(!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)&& !Character.isWhitespace(c)){ 
       String unicode = String.valueOf(c); 
       int code = (int)c; 
       if(!(code >= 0 && code <= 255)){ 
        unicode = Integer.toHexString(c); 
        if(high){ 
         high = false; 
         ss1 = Integer.parseInt(unicode, 16); 
        }else{ 
         high = true; 
         ss2 = Integer.parseInt(unicode, 16); 
         char chars = Character.toChars(ss1)[0]; 
         char chars2 = Character.toChars(ss2)[0]; 
         int codepoint = Character.toCodePoint(chars, chars2); 
         unicode = "Ax" + codepoint; 
         builder.append(unicode); 
        } 
       } 
      } 
      else{ 
       builder.append(c); 
      } 
     } 
     Log.i(TAG, builder.toString()); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return builder.toString(); 
} 

而對於解碼使用這個。

int emoji= Integer.parseInt(hexaCode.substring(2)); 
    reurn new String(Character.toChars(emoji)); 
0

escapeJavaString(String st)方法你正在得到unicode。您無法將其轉換爲codePonit,只需在開始時添加0x即可。看看this aswer,希望你找到一個關鍵

0
String str = edText.getText().toString(); 
String unicodeStr = StringEscapeUtils.escapeJava(str); 

您可以通過使用公琅2.5罐子

,如果你想獲得指數或十六進制值,則首先需要分析從表情符號得到的Unicode將字符串轉換爲HTML字符

StringEscapeUtils.escapeHtml() 

或使用emoji4j庫。

例如:

String line = "Hi , i am fine \uD83D\uDE02 \uD83D\uDE02, how r u ?"; 
EmojiUtils.hexHtmlify(line); //Hi , i am fine &#x1f602; &#x1f602;, how r u ? 

這裏1f602是你的十六進制代碼。

System.out.println(Character.toChars(Integer.parseInt("1f602",16)));