将Java读入十六进制

将Java读入十六进制,java,hex,Java,Hex,我曾试图解决这个问题,但我不断地提出一些毫无帮助的东西,我相信这很容易,当你知道怎么做的时候 我想做的是使用如下字节流读取文件: while((read = in.read()) != -1){ //code removed to save space Integer.toHexString(read); System.out.println(read); } 当它将十六进制打印到屏幕上时,它将打印出精细的数字,例如 31 13 12 0 但是当十六进制代码应

我曾试图解决这个问题,但我不断地提出一些毫无帮助的东西,我相信这很容易,当你知道怎么做的时候

我想做的是使用如下字节流读取文件:

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感谢您的帮助提前这个网站节省了我大量的研究,并学到了很多


非常感谢。

我想您需要的是格式化程序。尝试:

Formatter formatter = new Formatter();
formatter.format("%02x", your_int);
System.out.println(formatter.toString());

这就是你想要的吗?您的问题不太清楚,我想您可能从代码片段中删除了太多的代码。

我想您需要的是格式化程序。尝试:

Formatter formatter = new Formatter();
formatter.format("%02x", your_int);
System.out.println(formatter.toString());
这就是你想要的吗?您的问题不太清楚,我想您可能从代码片段中删除了太多的代码。

此方法:

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 
这是您要找的吗?

此方法:

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 

这就是你想要的吗?

大家好,谢谢回复,但我最终的方式是:

                        hexIn = in.read();
                        s = Integer.toHexString(hexIn);
                        if(s.length() < 2){
                            s = "0" + Integer.toHexString(hexIn);
                        }

我只是想以后我会像其他人一样发布它们,但非常感谢你们的帮助

大家好,我发了一条帖子,谢谢你们的回复,但我最终的做法是:

                        hexIn = in.read();
                        s = Integer.toHexString(hexIn);
                        if(s.length() < 2){
                            s = "0" + Integer.toHexString(hexIn);
                        }
import org.apache.commons.io.IOUtils;
import org.apache.commons.codec.binary.Hex;

InputStream is = new FileInputStream(new File("c:/file.txt"));
String hexString = Hex.encodeHexString(IOUtils.toByteArray(is));
我只是想以后我会像其他人一样发布它们,但非常感谢你们的帮助

import org.apache.commons.io.IOUtils;
import org.apache.commons.codec.binary.Hex;

InputStream is = new FileInputStream(new File("c:/file.txt"));
String hexString = Hex.encodeHexString(IOUtils.toByteArray(is));
在java 7中,您可以直接从文件中读取字节数组,如下所示:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path)     
在java 7中,您可以直接从文件中读取字节数组,如下所示:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path)     

要打印的所有值是否适合一个字节?您遇到的问题是0和150-F之间的数字是多少?要打印的所有值都适合一个字节?您遇到的问题是数字在0和150-F之间是什么?