четверг, 1 мая 2008 г.

Big Endian and Little Endian

Here my util class to manipulate with endians and java int/long





1 /***************************************************************************
2 * Copyright (C) 2007 by cy6ergn0m *
3 * *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 /*
21 * Endians.java
22 *
23 * Created on 21 june 2007, 0:03
24 *
25 */
26
27 package libim.utils.binary;
28
29 /**
30 *
31 * @author cy6erGn0m
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

Комментариев нет: