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
42
43
44
45
46
47 package jdbm.helper;
48
49
50
51
52
53
54
55
56 public class Conversion
57 {
58
59
60
61
62 public static byte[] convertToByteArray( String s )
63 {
64 try {
65
66
67 return s.getBytes( "UTF8" );
68 } catch ( java.io.UnsupportedEncodingException uee ) {
69 uee.printStackTrace();
70 throw new Error( "Platform doesn't support UTF8 encoding" );
71 }
72 }
73
74
75
76
77
78 public static byte[] convertToByteArray( byte n )
79 {
80 n = (byte)( n ^ ( (byte) 0x80 ) );
81 return new byte[] { n };
82 }
83
84
85
86
87
88 public static byte[] convertToByteArray( short n )
89 {
90 n = (short) ( n ^ ( (short) 0x8000 ) );
91 byte[] key = new byte[ 2 ];
92 pack2( key, 0, n );
93 return key;
94 }
95
96
97
98
99
100 public static byte[] convertToByteArray( int n )
101 {
102 n = (n ^ 0x80000000);
103 byte[] key = new byte[4];
104 pack4(key, 0, n);
105 return key;
106 }
107
108
109
110
111
112 public static byte[] convertToByteArray( long n )
113 {
114 n = (n ^ 0x8000000000000000L);
115 byte[] key = new byte[8];
116 pack8( key, 0, n );
117 return key;
118 }
119
120
121
122
123
124 public static String convertToString( byte[] buf )
125 {
126 try {
127
128
129 return new String( buf, "UTF8" );
130 } catch ( java.io.UnsupportedEncodingException uee ) {
131 uee.printStackTrace();
132 throw new Error( "Platform doesn't support UTF8 encoding" );
133 }
134 }
135
136
137
138
139
140 public static int convertToInt( byte[] buf )
141 {
142 int value = unpack4( buf, 0 );
143 value = ( value ^ 0x80000000 );
144 return value;
145 }
146
147
148
149
150
151 public static long convertToLong( byte[] buf )
152 {
153 long value = ( (long) unpack4( buf, 0 ) << 32 )
154 + ( unpack4( buf, 4 ) & 0xFFFFFFFFL );
155 value = ( value ^ 0x8000000000000000L );
156 return value;
157 }
158
159
160
161
162 static int unpack4( byte[] buf, int offset )
163 {
164 int value = ( buf[ offset ] << 24 )
165 | ( ( buf[ offset+1 ] << 16 ) & 0x00FF0000 )
166 | ( ( buf[ offset+2 ] << 8 ) & 0x0000FF00 )
167 | ( ( buf[ offset+3 ] << 0 ) & 0x000000FF );
168
169 return value;
170 }
171
172
173 static final void pack2( byte[] data, int offs, int val )
174 {
175 data[offs++] = (byte) ( val >> 8 );
176 data[offs++] = (byte) val;
177 }
178
179
180 static final void pack4( byte[] data, int offs, int val )
181 {
182 data[offs++] = (byte) ( val >> 24 );
183 data[offs++] = (byte) ( val >> 16 );
184 data[offs++] = (byte) ( val >> 8 );
185 data[offs++] = (byte) val;
186 }
187
188
189 static final void pack8( byte[] data, int offs, long val )
190 {
191 pack4( data, 0, (int) ( val >> 32 ) );
192 pack4( data, 4, (int) val );
193 }
194
195
196
197
198
199 public static void main( String[] args )
200 {
201 byte[] buf;
202
203 buf = convertToByteArray( (int) 5 );
204 System.out.println( "int value of 5 is: " + convertToInt( buf ) );
205
206 buf = convertToByteArray( (int) -1 );
207 System.out.println( "int value of -1 is: " + convertToInt( buf ) );
208
209 buf = convertToByteArray( (int) 22111000 );
210 System.out.println( "int value of 22111000 is: " + convertToInt( buf ) );
211
212
213 buf = convertToByteArray( (long) 5L );
214 System.out.println( "long value of 5 is: " + convertToLong( buf ) );
215
216 buf = convertToByteArray( (long) -1L );
217 System.out.println( "long value of -1 is: " + convertToLong( buf ) );
218
219 buf = convertToByteArray( (long) 1112223334445556667L );
220 System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) );
221 }
222
223 }