简体   繁体   English

以十六进制形式阅读Java

[英]Read Java in as Hex

I have tried to solve this but I keep coming up with stuff that is no help I'm sure this is easy (when you know how of course ;) ) 我已经尝试解决此问题,但我不断提出无济于事的方法,我确信这很容易(当您当然知道的时候;))

What I would like to do is read in a file using a byte stream like below: 我想做的是使用如下所示的字节流读取文件:

while((read = in.read()) != -1){

       //code removed to save space

       Integer.toHexString(read);
System.out.println(read);

}

When it prints out the Hex to the screen it will print out numbers fine eg 31 13 12 0 当它在屏幕上打印十六进制时,它将打印出很好的数字,例如31 13 12 0

but when it comes to a hex code that should be 01 31 it will print 0 131. I want to read it in to a variable like you would see in a hex editor ie 00 11 21 31 no single numbers as i need to scan the whole file and look for patterns which I know how to do I'm just stuck on this :/ 但是当涉及到应为01 31的十六进制代码时,它将打印0131。我想将其读入一个变量,就像您在十六进制编辑器中看到的那样,即00 11 21 31没有单个数字,因为我需要扫描整个文件,寻找我知道该怎么做的模式://

so in short i need a variabe to contain the two hex characters ie int temp = 01 not int temp = 0 , I hope this all makes sense, I'm a little confused as it's 3am! 简而言之,我需要一个可变参数来包含两个十六进制字符,即int temp = 01而不是int temp = 0,我希望一切都有意义,因为凌晨3点,我有点困惑!

If anyone knows how to do this I would be most greatful, ps thanks for the help in advance this site has saved me loads of research and have learnt a lot! 如果有人知道该怎么做,我将非常感激,ps感谢您在此站点之前提供的帮助,这为我节省了很多研究工作,并学到了很多东西!

Many thanks. 非常感谢。

This method : 此方法:

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");
        }
    }
}

will output something like this : 将输出如下内容:

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 

Is it what you are looking for? 是您要找的东西吗?

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));

In java 7 you can read byte array directly from file as below : 在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)     

I think what you're looking for is a formatter. 我认为您正在寻找的是格式化程序。 Try: 尝试:

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

Does that do what you're looking for? 这就是您想要的吗? Your question wasn't all that clear (and I think maybe you deleted too much code from your snippet). 您的问题不是很清楚(我认为您可能从摘要中删除了太多代码)。

Hi everyone one posted, thanks for the reply but the way I eneded up doing it was: 大家好,一个人发帖,谢谢您的回复,但是我坚决地做到了:

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

Just thought I would post they way I did it for anyone else in future, thank you soo much for your help though! 只是以为我以后会以其他人的方式发布,所以非常感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM