2017-12-09 6 views
0

私が直接Unicodeを入力するなどの方法でStringに変換する場合、私は絵文字を見ることができます:stringbufferを文字列に変換しましたが、stringをunicode intに変換する方法はありますか?

int intUnicode = 0x1F601; 

public String getEmojiByUnicode(int unicode) { 
    return new String(Character.toChars(unicode)); 
} 

しかし、私はちょうどユニコード値のようなStringときに変換する方法のまわりで私の頭をラップすることはできません。私は直接それをやったようIntegerにそれをひそかする方法

String strUnicode = "0xf601"; 

Integer.parseInt(strUnicode)コンパイラを使用しようとすると、毎回エラーが発生します。

InputStream is = assetManager.open("emoji_unicode.txt"); 
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
if (is != null){ 
    try { 
     while ((data = reader.readLine()) != null){ 
      sbuffer.append(data + "\n"); 
      String j = sbuffer.toString().trim(); 
      int k = Integer.parseInt(j,16); 
      String l = getEmojiByUnicode(k); 
      emoArray.add(l); 
     } 
     btn.setText(emoArray.get(5)); 
     is.close(); 
    }catch (IOException e){ 
     e.printStackTrace(); 
    } 
} 

非常に難しいことではありませんが、私は非常に近いと思っていますが、それでも私には届きません。どんな助けでも大歓迎です。 リガード

+0

あなたのUnicode文字列はあなたの最初の例で生成したものと同じではありません。あなたは[UTF-16](https://en.wikipedia.org/wiki/UTF-16)エンコーディングを見て、それが[あなたが使っている範囲]のコードポイントにどのように適用されるかを見てください(https: //en.wikipedia.org/wiki/UTF-16#U+10000_to_U+10FFFF)。あなたが与えている文字列が有効なUTF-16でないため、あなたのコンパイラは不平を言っています。 – Jules

+0

[16進文字列を整数に解析すると、NumberFormatExceptionがスローされる可能性がありますか?](https://stackoverflow.com/questions/11377944/parsing-a-hexadecimal-string-to-an-integer-throws-a-numberformatexception) –

答えて

0

あなたは(Unicodeの「エンコーディング」は16進数です)文字列を解析しようとしている、とInteger.parseInt()はそうするためのいくつかの奇妙な制限があります。この答えを見てみましょう:あなたが開始している

Parsing a Hexadecimal String to an Integer throws a NumberFormatException?

+0

ありがとうございます!あなたのリンクは '.decode'を見つけるのを助けました:D – Meimo

関連する問題