Here my util class to manipulate with endians and java int/long
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 package libim.utils.binary;
28
29
30
31 @author
32
33 public abstract class Endians {
34
35 public static final byte[] int16_to_big_endian ( int number ) {
36 byte[] result = new byte[ 2 ];
37 result[1] = (byte) ( number % 256 );
38 result[0] = (byte) ( ( number & 0xff00 ) >> 8 );
39 return result;
40 }
41
42 public static final byte[] int16_to_little_endian ( int number ) {
43 byte[] result = new byte[ 2 ];
44 result[0] = (byte) ( number % 256 );
45 result[1] = (byte) ( ( number & 0xff00 ) >> 8 );
46 return result;
47 }
48
49 public static final byte[] int32_to_big_endian ( int number ) {
50 byte[] result = new byte[ 4 ];
51 result[3] = (byte) ( number % 256 );
52 result[2] = (byte) ( ( number & 0xff00 ) >> 8 );
53 result[1] = (byte) ( ( number & 0xff0000 ) >> 16 );
54 result[0] = (byte) ( ( number & 0xff000000 ) >> 24 );
55 return result;
56 }
57
58 public static final byte[] int32_to_little_endian ( int number ) {
59 byte[] result = new byte[ 4 ];
60 result[0] = (byte) ( number % 256 );
61 result[1] = (byte) ( ( number & 0xff00 ) >> 8 );
62 result[2] = (byte) ( ( number & 0xff0000 ) >> 16 );
63 result[3] = (byte) ( ( number & 0xff000000 ) >> 24 );
64 return result;
65 }
66
67 public static final byte[] int64_to_big_endian ( long number ) {
68 byte[] result = new byte[ 8 ];
69 for ( int i = 7 ; i >= 0 ; i-- ) {
70 result[i] = (byte) ( number & 0xff );
71 number >>= 8;
72 }
73 return result;
74 }
75
76 public static final byte[] int64_to_little_endian ( long number ) {
77 byte[] result = new byte[ 8 ];
78 for ( int i = 0 ; i < 8 ; i++ ) {
79 result[i] = (byte) ( number & 0xff );
80 number >>= 8;
81 }
82 return result;
83 }
84
85 public static final int byte2int ( byte b ) {
86 return ( b < 0 ) ? ( 256 + b ) : b;
87 }
88
89 public static final int byte2int ( int b ) {
90 return ( b < 0 ) ? ( 256 + b ) : b;
91 }
92
93 public static final int big_endian_to_int16 ( byte[] bytes ) {
94 if ( bytes.length == 0 ) {
95 return 0;
96 } else if ( bytes.length == 1 ) {
97 return byte2int( bytes[0] );
98 } else {
99 return ( byte2int( bytes[0] ) << 8 ) | byte2int( bytes[1] );
100 }
101 }
102
103 public static final int little_endian_to_int16 ( byte[] bytes ) {
104 if ( bytes.length == 0 ) {
105 return 0;
106 } else if ( bytes.length == 1 ) {
107 return byte2int( bytes[0] );
108 } else {
109 return ( byte2int( bytes[1] ) << 8 ) + byte2int( bytes[0] );
110 }
111 }
112
113 public static final int big_endian_to_int32 ( byte[] bytes ) {
114 if ( bytes.length < 4 ) {
115 int result = 0;
116 for ( byte b : bytes ) {
117 result = ( result << 8 ) | b;
118 }
119 return result;
120 } else {
121 return ( byte2int( bytes[0] ) << 24 ) | ( byte2int( bytes[1] ) << 16 ) | ( byte2int( bytes[2] ) << 8 ) | ( byte2int( bytes[3] ) );
122 }
123 }
124
125 public static final int little_endian_to_int32 ( byte[] bytes ) {
126 if ( bytes.length < 4 ) {
127 int result = 0;
128 for ( byte b : bytes ) {
129 result = ( result >> 8 ) | ( b << 24 );
130 }
131 return result;
132 } else {
133 return ( byte2int( bytes[3] ) << 24 ) | ( byte2int( bytes[2] ) << 16 ) | ( byte2int( bytes[1] ) << 8 ) | ( byte2int( bytes[0] ) );
134 }
135 }
136
137 public static final long big_endian_to_int64 ( byte[] bytes ) {
138 long result = 0;
139 for ( byte b : bytes ) {
140 result = ( result << 8 ) | b;
141 }
142 return result;
143 }
144
145 public static final long little_endian_to_int64 ( byte[] bytes ) {
146 long result = 0;
147 for ( byte b : bytes ) {
148 result = ( result >> 8 ) | ( b << 24 );
149 }
150 return result;
151 }
152 }
153
154
Комментариев нет:
Отправить комментарий