Java,使用Scanner将字符输入为UTF-8,无法打印文本 [英] Java, Using Scanner to input characters as UTF-8, can't print text

查看:78
本文介绍了Java,使用Scanner将字符输入为UTF-8,无法打印文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将String转换为Array作为UTF-8,但是我不能像第一个String那样将它转换回String。

I can convert String to Array as UTF-8, but I can't convert it back to String like the first String.

public static void main(String[] args) {

    Scanner h = new Scanner(System.in);
    System.out.println("INPUT : ");
    String stringToConvert = h.nextLine();
    byte[] theByteArray = stringToConvert.getBytes();

    System.out.println(theByteArray);
    theByteArray.toString();
    String s = new String(theByteArray);

    System.out.println(""+s);
}

如何打印 theByteArray 作为字符串?

推荐答案

String s = new String(theByteArray);

应该是

String s = new String(theByteArray, Charset.forName("UTF-8"));

这里的根本问题是String构造函数不聪明。 String构造函数无法区分正在使用的字符集,并尝试使用系统标准(通常类似于ASCII或ISO-8859-1)对其进行转换。这就是正常的A-Za-z看起来正常然后其他一切开始失败的原因。

The underlying issue here is that String constructors aren't smart. The String constructor cannot distinguish the charset that is being used and will try to convert it using the system standard which is generally something like ASCII or ISO-8859-1. This is why normal A-Za-z looks proper but then everything else begins to fail.

字节是从-127到127的类型,因此对于UTF-8转换连续字节需要连接。 String构造函数不可能将其与字节数组区分开来,因此默认情况下它将单独处理每个字节(因此,为什么基本的字母数字将始终工作,因为它们属于此范围)。

byte is a type that runs from -127 to 127 thus for UTF-8 conversion consecutive bytes need to be concatenated. It's impossible for the String constructor to distinguish this off a byte array so it will handle each byte individually by default (thus why basic alphanumeric will always work as they fall into this range).

示例:

String text = "こんにちは";
byte[] array = text.getBytes("UTF-8");
String s = new String(array, Charset.forName("UTF-8"));
System.out.println(s); // Prints as expected
String sISO = new String(array, Charset.forName("ISO-8859-1")); // Prints 'ããã«ã¡ã¯'
System.out.println(sISO);

这篇关于Java,使用Scanner将字符输入为UTF-8,无法打印文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆