我有一个包含值-110(和其他负值)的byte []。当我将其转换为字符串时,我需要它显示’(正确的单引号)。目前,我收到一个问号(?)

“”与this page中提到的特殊ASCII字符#146对齐,但是我现在对如何输入-110或146(-110 + 256)并成为’值感到困惑。我也做到了,任何建议将不胜感激。

byte[] b = {-110,84};
System.out.println(new String(b, Charset.forName("Windows-1252"))); //Displays ?T . The desired output should be ’T
System.out.println(new String(b, Charset.forName("UTF-8"))); //Displays ?T . The desired output should be ’T
System.out.println(new String(b, Charset.forName("ISO-8859-1"))); //Displays ?T . The desired output should be ’T

最佳答案

感谢John Skeet在答复中指出的答复,该Java程序需要识别输入数据Windows-1252,并且Windows命令行也未设置到代码页。

通过运行将命令行代码页设置为Windows-1252

chcp 1252


通过添加以下参数来启动Java程序以将Windows-1252用作默认值

-Dfile.encoding="Windows-1252"

10-08 18:50