我有一个包含值-110(和其他负值)的字节[]。当我将其转换为字符串时,我需要它显示一个’(右单引号)。目前,我收到一个问号(?)
’ 与 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"
关于Java 字符编码为 HTML ISO-8859-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47157483/