java 如何在java中将unicode字符串转换为ASCII

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15356716/
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-10-31 19:20:22  来源:igfitidea点击:

how can I convert unicode string to ASCII in java

javaandroidunicodeascii

提问by PPShein

I'm now trying to convert unicode font to ascii in android. I wrote following coding to convert unicode font to ascii but it's failed. Because result cannot display properly after being converted.

我现在正在尝试将 unicode 字体转换为 android 中的 ascii。我编写了以下代码将 unicode 字体转换为 ascii,但失败了。因为转换后结果无法正常显示。

unicode font = '??????????????' to something like '\u100F\u1039\u100D'

unicode 字体 = '????????????????' 类似于 '\u100F\u1039\u100D'

public static String toJAVA (String zawgyi) {
    String output = "";
    char[] charArray = zawgyi.toCharArray();

    for (int i = 0; i < charArray.length; i++) {
        char a = charArray[i];
        if ((int) a > 255) {
            output += "\u" + Integer.toHexString((int) a) + "--";
        } else {
            output += a;
        }
    }       
    return output;
}

采纳答案by stinepike

use java.text.Normalizer class to convert from unicode to ascii. here is a sample code from the answer https://stackoverflow.com/a/2097224/931982

使用 java.text.Normalizer 类将 unicode 转换为 ascii。这是答案https://stackoverflow.com/a/2097224/931982 中的示例代码

String s = "口水雞 hello ?";

String s1 = Normalizer.normalize(s, Normalizer.Form.NFKD);
String regex = Pattern.quote("[\p{InCombiningDiacriticalMarks}\p{IsLm}\p{IsSk}]+");

String s2 = new String(s1.replaceAll(regex, "").getBytes("ascii"), "ascii");

System.out.println(s2);
System.out.println(s.length() == s2.length());