1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Htonl{
    public static void main(String args[]){
        if(args.length<1){
            System.out.println("Usage : java Htonl");
            System.exit(0);
        }
 
        int value=Integer.parseInt(args[0]);
        int newValue=swap(value);
 
        System.out.println("big endian value : 0x" + Integer.toHexString(value));
        System.out.println("little endian value : 0x" + Integer.toHexString(newValue));
    }
 
    /*
     * Swapping byte orders of given numeric types
     */
    static short swap(short x){
        return (short)((x << 8) | ((x >> 8) & 0xff));
    }
 
    static char swap(char x){
        return (char)((x << 8) | ((x >> 8) & 0xff));
    }
 
    static int swap(int x){
        return (int)((swap((short)x) << 16) | (swap((short)(x >> 16)) & 0xffff));
    }
 
    static long swap(long x){
        return (long)(((long)swap((int)(x)) << 32) | ((long)swap((int)(x >> 32)) & 0xffffffffL));
    }
 
    static float swap(float x){
        return Float.intBitsToFloat(swap(Float.floatToRawIntBits(x)));
    }
 
    static double swap(double x){
        return Double.longBitsToDouble(swap(Double.doubleToRawLongBits(x)));
    }
}

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

간단한 랜덤함수 사용법  (0) 2015.04.29
stream read하기  (0) 2015.02.18
java Mashalling과 Serialization의 차이  (0) 2015.01.31
Java Serializable : 자바 직렬화  (0) 2015.01.31
thread이야기_02.run()과 start()의 차이  (0) 2014.11.23
Posted by duehd88
,