简体   繁体   English

如何从ASCII转换为字符串

[英]How do I convert from ASCII to String

I am trying to parse an ascii list to a string. 我正在尝试将ascii列表解析为字符串。 The problem is that with some special chars, I have torubles. 问题在于,使用一些特殊的字符,我有面包。 If I try to parse this: 如果我尝试解析此:

115 097 116 195 168 108 194 183 108 105 116

, the result sould be "satèl·lit". ,结果将是“satèl·lit”。 The code I am using to parse it is : 我用来解析的代码是:

ASCIIList.add(Character.toString((char) Integer.parseInt(asciiValue)));

But the result is satèl·lit. 但是结果令人满意。 I saw that for example "è" -> "195 168". 我看到例如“è”->“ 195 168”。 I do not know how to parse it correctly. 我不知道如何正确解析它。

Assuming you already have split the input into an array of string, the code could look like so: 假设您已经将输入拆分为字符串数组,则代码如下所示:

String convertToString(String[] numberArray) {
    byte[] utf8Bytes = new byte[numberArray.length];
    for (int i = 0; i < numberArray.length; i++) {
        utf8Bytes[i] = (byte) Integer.parseInt(numberArray[i]);
    }
    return new String(utf8Bytes, StandardCharsets.UTF_8);
}

So each number becomes a bytes. 因此,每个数字都变成一个字节。 The entire array of bytes is then converted into a string using UTF-8 charset. 然后,使用UTF-8字符集将整个字节数组转换为字符串。

UTF-8 uses multiple bytes to represent characters outside the ASCII range. UTF-8使用多个字节来表示ASCII范围之外的字符。 In your example it affects "è" and "·". 在您的示例中,它会影响“è”和“·”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM