java 将字符串转换为 unicode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33871966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:13:00  来源:igfitidea点击:

Convert string to unicode

javaandroidunicodechar

提问by mGol

i want convert a simple text like, "my simple text" to Unicode character in Android.

我想在 Android 中将“我的简单文本”之类的简单文本转换为 Unicode 字符。

for example this :

例如这个:

"\u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06cc\u0633\u06cc"

“\u0628\u0631\u0646\u0627\u0645\u0647\u0646\u0648\u06cc\u0633\u06cc”

this string in uni code is :

uni 代码中的这个字符串是:

?????? ?????

???????????

i want input

我要输入

?????? ?????

???????????

and response

和回应

"\u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06cc\u0633\u06cc"

“\u0628\u0631\u0646\u0627\u0645\u0647\u0646\u0648\u06cc\u0633\u06cc”

this string . please help me .

这个字符串。请帮我 。

采纳答案by Javierif

Android native its java, so.. check this:

Android 原生它的 java,所以.. 检查这个:

You can do it for any Java char using the one liner here:

您可以使用此处的单行对任何 Java 字符执行此操作:

System.out.println( "\u" + Integer.toHexString('÷' | 0x10000).substring(1) );

Reference: Get unicode value of a character

参考:获取字符的 unicode 值

I hope that I have helped answer some of your questions.

我希望我已经帮助回答了你的一些问题。

回答by Abhash Upadhyaya

Use Apache Commons Lang api. StringEscapeUtils.escapeJava() can help you get the answer.

使用 Apache Commons Lang api。StringEscapeUtils.escapeJava() 可以帮助您找到答案。

import org.apache.commons.lang3.StringEscapeUtils;

public class StringUnicode {

    public static void main(String[] args) {

        String foreignText = "?????? ?????";

        String response = StringEscapeUtils.escapeJava(foreignText);
        System.out.println(response);
    }
}