public static int swapBigEndian(String args) {

        int value=Integer.parseInt(args);

        // all java integer is BIG-ENDIAN(network byte-order)
        byte[] bytes=new byte[4];
        bytes[3]=(byte)((value&0xFF000000)>>24);
        bytes[2]=(byte)((value&0x00FF0000)>>16);
        bytes[1]=(byte)((value&0x0000FF00)>>8);
        bytes[0]=(byte) (value&0x000000FF);


        int newValue = 0;
        newValue |= (((int)bytes[0])<<24)&0xFF000000;
        newValue |= (((int)bytes[1])<<16)&0xFF0000;
        newValue |= (((int)bytes[2])<<8)&0xFF00;
        newValue |= (((int)bytes[3]))&0xFF;

        return newValue;
}

'java > util' 카테고리의 다른 글

[java] - int to byte array  (0) 2010.06.29
[java] - Big Endian to Little Endian  (0) 2010.06.29
[java] - float to byte array  (1) 2010.06.29
[java] - byte array to float  (0) 2010.06.29
[java] - byte array to short  (0) 2010.06.29
Posted by 짱가쟁이