Converting Little Endian to Big Endian

Viewed 69337

All,

I have been practicing coding problems online. Currently I am working on a problem statement Problems where we need to convert Big Endian <-> little endian. But I am not able to jot down the steps considering the example given as:

123456789 converts to 365779719

The logic I am considering is :
1 > Get the integer value (Since I am on Windows x86, the input is Little endian)
2 > Generate the hex representation of the same.
3 > Reverse the representation and generate the big endian integer value

But I am obviously missing something here.

Can anyone please guide me. I am coding in Java 1.5

7 Answers

Java primitive wrapper classes support byte reversing since 1.5 using reverseBytes method.

Short.reverseBytes(short i)
Integer.reverseBytes(int i)
Long.reverseBytes(long i)

Just a contribution for those who are looking for this answer in 2018.

Just use the static function (reverseBytes(int i)) in java which is under Integer Wrapper class

Integer i=Integer.reverseBytes(123456789);
System.out.println(i);

output:

365779719
Related