有没有办法在 java 中显示 ASCII 符号?

Is there a way to display ASCII symbols in java?

我正在查看一个捕获文本文件,它显示了从 ASCII 十六进制值中获得的符号。在 Notepad++ 中,它还显示了 00 to 1F 中的符号,它们被 this webpage and you can find the full ASCII table including the extended values with their hexadecimal and decimal numbers here 很好地映射了出来。我知道它们是控制字符,但有什么方法可以在 java?

中显示它们

这是文件中显示的示例

我可以让所有其他 ASCII 符号显示在我的 java 程序中,但不能显示带有 STX NUL EOT SOH 等符号的符号。它们是来自 [=14= 的值] 为十进制,00 to 1F 为十六进制。

这是来自 here 的 ASCII table 的快照

这是控制台中显示内容的控制台片段

这是我用来打印的代码。

FileInputStream fis = new FileInputStream(filePath);
int length = (int) new File(filePath).length();
byte[] buffer = new byte[length];
fis.read(buffer, 0, length);

for (int i = 1; i < hex.length(); i++){
                if(i %2 == 0){
                    String test = String.valueOf(hex.charAt(i-2))+ String.valueOf(hex.charAt(i-1));

                    System.out.println("Hex: "+test+" hexadecimal: "+Integer.parseInt(test, 16)+" char: "+(char) Integer.parseInt(test, 16));
                }
            }

任何帮助将不胜感激!!

这里的问题是您将值显示为字符,因此您的控制台会自动将其解释为具有特定显示方式的字符。

如果您想显示与记事本视图相匹配的内容,您可能必须对非 printable 字符使用 table,它看起来像:

private static final String[] myTable = {"NUL", "SOH", ... };
public String getCharAsString(char value) {
    if(value <= (char) 0x1F) {
        return myTable[value];
    } else {
        return "" + value;
    }
}