简体   繁体   English

无法在 Windows 控制台中正确打印非英文字符,如 ë

[英]Can't properly print non-English characters like ë in Windows console

For some weird reason I can't seem to print ë in Java.出于某种奇怪的原因,我似乎无法在 Java 中打印 ë。

public class Eindopdracht0002test
{
  public static void main(String[] args)
  {
    System.out.println("\u00EB");
  }
}  

It's supposed to print "België" (dutch for Belgium) however it returns "Belgi├½" .它应该打印"België" (比利时的荷兰语)但它返回"Belgi├½"

Does anyone know how to resolve this?有谁知道如何解决这个问题?

In UTF-8 ë is written as 11000011 10101011 (source: https://unicode-table.com/en/00EB ).在 UTF-8 中, ë写为11000011 10101011 (来源: https : 11000011 10101011 )。
Console in Windows is using code pages which are 8-bit mappings to characters (you can check code page of your console with chcp command). Windows 中的控制台使用的代码页是 8 位字符映射(您可以使用chcp命令检查控制台的代码页)。 This means when ë is sent to output stream (console) as 11000011 10101011 bits, console sees it as two characters, which in 850 code page (based on your comments) are mapped to:这意味着当ë作为11000011 10101011位发送到输出流(控制台)时,控制台将其视为两个字符,在850 代码页(根据您的评论)中映射到:

  • - 11000011 (195 in decimal) - 11000011(十进制195)
  • ½ - 10101011 (171 in decimal) ½ - 10101011(十进制 171)

If you don't want to use UTF-8 encoding you can create separate Writer and specify different encoding which will translate characters to bytes according to that encoding.如果您不想使用 UTF-8 编码,您可以创建单独的 Writer 并指定不同的编码,这将根据该编码将字符转换为字节。 To do so you can use为此,您可以使用

OutputStreamWriter(OutputStream out, String charsetName)

which in your case may look like在你的情况下可能看起来像

OutputStreamWriter(System.out, "cp850") osw = OutputStreamWriter(System.out, "cp850");
//  needed encoding ------------^^^^^

since you want send characters with specified encoding to standard output stream.因为您想将具有指定编码的字符发送到标准输出流。

To use println method and ensure it will automatically flush its data you can wrap created OutputStreamWriter in要使用println方法并确保它会自动刷新其数据,您可以将创建的OutputStreamWriter包装在

PrintWriter(OutputStream out, boolean autoFlush)

like喜欢

PrintWriter out = new PrintWriter(osw, true);

You can also do both these things in one line:您也可以在一行中完成以下两项操作:

PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, "cp850"), true);

Now if you use out.println("\ë");现在如果你使用out.println("\ë"); it should use recognize ë character and use cp850 encoding to locate its mapping (which is 137 ) and send proper byte representation (here 10001001 ) to System.out (console).它应该使用识别ë字符并使用cp850编码来定位其映射(即137 )并将正确的字节表示(此处为10001001 )发送到System.out (控制台)。

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

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