Java将字符串转换为Unicode字符。“U+1F600”=????

时间:2021-09-18 23:09:58

Please note that this question is not a duplicate.

请注意,这个问题不是重复的。

I have a String like this:

我有一个这样的字符串:

// My String
String myString = "U+1F600";
// A method to convert the String to a real character
String unicodeCharacter = convertStringToUnicode(myString);
// Then this should print: ????
System.out.println(unicodeCharacter);

How can I convert this String to the unicode character ????? I then want to show this in a TextView.

我怎样才能将这个字符串转换为unicode字符?????吗然后我想在TextView中显示这个。

2 个解决方案

#1


3  

What you are trying to do is to print the unicode when you know the code but as String... the normal way to do this is using the method

你所要做的是在你知道代码的时候打印unicode,但是作为字符串…通常的方法是使用方法。

Character.toChars(int)

Character.toChars(int)

like:

如:

System.out.print(Character.toChars(0x1f600));

now in you case, you have

现在你有了。

String myString = "U+1F600";

so you can truncate the string removing the 1st 2 chars, and then parsing the rest as an integer with the method Integer.parseInt(valAsString, radix)

因此,您可以截断删除第1个字符的字符串,然后将剩余的字符串解析为带有方法整数的整数。方法(valAsString基数)

Example:

String myString = "U+1F600";
System.out.print(Character.toChars(Integer.parseInt(myString.substring(2), 16)));

#2


1  

Try

试一试

yourTextVIew.setText(new String(Character.toChars(0x1F60B)));

#1


3  

What you are trying to do is to print the unicode when you know the code but as String... the normal way to do this is using the method

你所要做的是在你知道代码的时候打印unicode,但是作为字符串…通常的方法是使用方法。

Character.toChars(int)

Character.toChars(int)

like:

如:

System.out.print(Character.toChars(0x1f600));

now in you case, you have

现在你有了。

String myString = "U+1F600";

so you can truncate the string removing the 1st 2 chars, and then parsing the rest as an integer with the method Integer.parseInt(valAsString, radix)

因此,您可以截断删除第1个字符的字符串,然后将剩余的字符串解析为带有方法整数的整数。方法(valAsString基数)

Example:

String myString = "U+1F600";
System.out.print(Character.toChars(Integer.parseInt(myString.substring(2), 16)));

#2


1  

Try

试一试

yourTextVIew.setText(new String(Character.toChars(0x1F60B)));