我试图解决这个问题,但我一直想出一些没有帮助的东西我相信这很容易(当然,当你知道怎么做的时候 ;))
我想做的是使用如下字节流读取文件:
while((read = in.read()) != -1){
//code removed to save space
Integer.toHexString(read);
System.out.println(read);
}
当它在屏幕上打印十六进制时,它会打印出数字,例如 31 13 12 0
但是当涉及到应该为 01 31 的十六进制代码时,它将打印 0 131。我想将它读入一个变量,就像您在十六进制编辑器中看到的那样,即 00 11 21 31 没有我需要的单个数字扫描整个文件并寻找我知道该怎么做的模式我只是坚持这个:/
所以简而言之,我需要一个变量来包含两个十六进制字符,即 int temp = 01 而不是 int temp = 0,我希望这一切都有意义,我有点困惑,因为现在是凌晨 3 点!
如果有人知道如何做到这一点,我将不胜感激,p.s 感谢您提前提供的帮助,该网站为我节省了大量的研究工作并学到了很多东西!
非常感谢。
最佳答案
这个方法:
public static void printHexStream(final InputStream inputStream, final int numberOfColumns) throws IOException{
long streamPtr=0;
while (inputStream.available() > 0) {
final long col = streamPtr++ % numberOfColumns;
System.out.printf("%02x ",inputStream.read());
if (col == (numberOfColumns-1)) {
System.out.printf("\n");
}
}
}
将输出如下内容:
40 32 38 00 5f 57 69 64 65 43
68 61 72 54 6f 4d 75 6c 74 69
42 79 74 65 40 33 32 00 5f 5f
69 6d 70 5f 5f 44 65 6c 65 74
65 46 69 6c 65 41 40 34 00 5f
53 65 74 46 69 6c 65 50 6f 69
6e 74 65 72 40 31 36 00 5f 5f
69 6d 70 5f 5f 47 65 74 54 65
6d 70 50 61 74 68 41 40 38 00
这是你要找的吗?
关于java - 以十六进制读入 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4703503/