Search code examples
javafile-iohexdatainputstream

Java get hex reversed


I'm trying to read a file like that:

private File infile;
private FileInputStream fis;
private DataInputStream dis;

and

infile = new File("myfile");
fis = new FileInputStream(infile);
dis = new DataInputStream(fis);

Now I want to read a hex like that:

int current = dis.readInt();
System.out.println("0x" + Integer.toHexString(current));

For anybody who needs it: here are the first bytes of my file

3412 aa55 0200 0000

The problem is that my output is 0x3412aa55 but it should be 0x55aa1234. What can I do to fix this?


Solution

  • Use Integer.reverseBytes() to reverse the bytes.