das_j
das_j

Reputation: 4794

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?

Upvotes: 0

Views: 1403

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

Use Integer.reverseBytes() to reverse the bytes.

Upvotes: 4

Related Questions