Share this page 

Deal with big-endian and little-endian orderTag(s): String/Number


Java virtual machine always used big-endian, Intel x86 used little-endian.
public class Swab {
  public final static int swabInt(int v) {
    return  (v >>> 24) | (v << 24) |
      ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00);
  }

  public static void main(String argv[]) {
    //  before 0x01020304
    //    after  0x04030201
    int v = 0x01020304;
    System.out.println("before : 0x" + Integer.toString(v,16));
    System.out.println("after  : 0x" + Integer.toString(swabInt(v),16));
  }
}
NOTE: to convert UNSIGNED byte to integer, look at this How-to