// Java program to switch the byte ordering of a 32 bit two's complement int public class byteordering { static int switchbytes(int x) { long y = (long)x; // use a 64 bit int to do the work because of sign if (x<0) y = y + 0xffffffff; // convert to unsigned number short lsb = y % 256; // least significant byte short l2b = (y/256) % 256; // 2nd least significant byte short m2b = (y/256/256) % 256; // 2nd most signifiatn byte short msb = y/256/256/256; // most significant byte y = msb*0x00ffffff + m2b*0x0000ffff + l2b*256 + lsb; if (y>0x7fffffff) y = y-0xffffffff; return (int)y; } public static void main(String[] argv) { int x = Integer.parseInt(argv[0]); int xr = switchbytes(x); System.out.printf("reversed: %x",xr); } }