以十六进制读取 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4703503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:36:34  来源:igfitidea点击:

Read Java in as Hex

javahex

提问by James

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 e.g 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 i.e 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 的十六进制代码时,它会打印 0 131。我想将它读入一个变量,就像你在十六进制编辑器中看到的那样,即 00 11 21 31 没有单个数字,因为我需要扫描整个文件并寻找我知道该怎么做的模式我只是坚持这个:/

so in short i need a variabe to contain the two hex characters i.e 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, p.s thanks for the help in advance this site has saved me loads of research and have learnt a lot!

如果有人知道如何做到这一点,我将非常感激,ps 提前感谢您的帮助,这个网站为我节省了大量的研究工作,并且学到了很多东西!

Many thanks.

非常感谢。

回答by Tony

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?

这是您要找的吗?

回答by Ankit Singla

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)     

回答by James

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!

只是想我以后会以我为其他人做的方式发布它们,但非常感谢您的帮助!

回答by Raph Levien

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

这是否符合您的要求?你的问题不是很清楚(我想你可能从代码片段中删除了太多代码)。