Java 基礎 byte[]與各種數據類型互相轉換的簡單示例
這里對byte[]類型對long,int,double,float,short,cahr,object,string類型相互轉換的實例,
在socket開發過程中,通常需要將一些具體的值(這些值可能是各種Java類型)轉化為byte[]類型,為此我總結了如下這個示例,貼出來,以便經常翻看:
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
public class TestCase { /** * short到字節數組的轉換. */ public static byte [] shortToByte( short number) { int temp = number; byte [] b = new byte [ 2 ]; for ( int i = 0 ; i < b.length; i++) { b[i] = new Integer(temp & 0xff ).byteValue(); // 將最低位保存在最低位 temp = temp >> 8 ; // 向右移8位 } return b; } /** * 字節數組到short的轉換. */ public static short byteToShort( byte [] b) { short s = 0 ; short s0 = ( short ) (b[ 0 ] & 0xff ); // 最低位 short s1 = ( short ) (b[ 1 ] & 0xff ); s1 <<= 8 ; s = ( short ) (s0 | s1); return s; } /** * int到字節數組的轉換. */ public static byte [] intToByte( int number) { int temp = number; byte [] b = new byte [ 4 ]; for ( int i = 0 ; i < b.length; i++) { b[i] = new Integer(temp & 0xff ).byteValue(); // 將最低位保存在最低位 temp = temp >> 8 ; // 向右移8位 } return b; } /** * 字節數組到int的轉換. */ public static int byteToInt( byte [] b) { int s = 0 ; int s0 = b[ 0 ] & 0xff ; // 最低位 int s1 = b[ 1 ] & 0xff ; int s2 = b[ 2 ] & 0xff ; int s3 = b[ 3 ] & 0xff ; s3 <<= 24 ; s2 <<= 16 ; s1 <<= 8 ; s = s0 | s1 | s2 | s3; return s; } /** * long類型轉成byte數組 */ public static byte [] longToByte( long number) { long temp = number; byte [] b = new byte [ 8 ]; for ( int i = 0 ; i < b.length; i++) { b[i] = new Long(temp & 0xff ).byteValue(); // 將最低位保存在最低位 temp = temp // >> 8;// 向右移8位 } return b; } /** * 字節數組到long的轉換. */ public static long byteToLong( byte [] b) { long s = 0 ; long s0 = b[ 0 ] & 0xff ; // 最低位 long s1 = b[ 1 ] & 0xff ; long s2 = b[ 2 ] & 0xff ; long s3 = b[ 3 ] & 0xff ; long s4 = b[ 4 ] & 0xff ; // 最低位 long s5 = b[ 5 ] & 0xff ; long s6 = b[ 6 ] & 0xff ; long s7 = b[ 7 ] & 0xff ; // s0不變 s1 <<= 8 ; s2 <<= 16 ; s3 <<= 24 ; s4 <<= 8 * 4 ; s5 <<= 8 * 5 ; s6 <<= 8 * 6 ; s7 <<= 8 * 7 ; s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7; return s; } /** * double到字節數組的轉換. */ public static byte [] doubleToByte( double num) { byte [] b = new byte [ 8 ]; long l = Double.doubleToLongBits(num); for ( int i = 0 ; i < 8 ; i++) { b[i] = new Long(l).byteValue(); l = l >> 8 ; } return b; } /** * 字節數組到double的轉換. */ public static double getDouble( byte [] b) { long m; m = b[ 0 ]; m &= 0xff ; m |= (( long ) b[ 1 ] << 8 ); m &= 0xffff ; m |= (( long ) b[ 2 ] << 16 ); m &= 0xffffff ; m |= (( long ) b[ 3 ] << 24 ); m &= 0xffffffffl; m |= (( long ) b[ 4 ] << 32 ); m &= 0xffffffffffl; m |= (( long ) b[ 5 ] << 40 ); m &= 0xffffffffffffl; m |= (( long ) b[ 6 ] << 48 ); m &= 0xffffffffffffffl; m |= (( long ) b[ 7 ] << 56 ); return Double.longBitsToDouble(m); } /** * float到字節數組的轉換. */ public static void floatToByte( float x) { //先用 Float.floatToIntBits(f)轉換成int } /** * 字節數組到float的轉換. */ public static float getFloat( byte [] b) { // 4 bytes int accum = 0 ; for ( int shiftBy = 0 ; shiftBy < 4 ; shiftBy++ ) { accum |= (b[shiftBy] & 0xff ) << shiftBy * 8 ; } return Float.intBitsToFloat(accum); } /** * char到字節數組的轉換. */ public static byte [] charToByte( char c){ byte [] b = new byte [ 2 ]; b[ 0 ] = ( byte ) ((c & 0xFF00 ) >> 8 ); b[ 1 ] = ( byte ) (c & 0xFF ); return b; } /** * 字節數組到char的轉換. */ public static char byteToChar( byte [] b){ char c = ( char ) (((b[ 0 ] & 0xFF ) << 8 ) | (b[ 1 ] & 0xFF )); return c; } /** * string到字節數組的轉換. */ public static byte [] stringToByte(String str) throws UnsupportedEncodingException{ return str.getBytes( "GBK" ); } /** * 字節數組到String的轉換. */ public static String bytesToString( byte [] str) { String keyword = null ; try { keyword = new String(str, "GBK" ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return keyword; } /** * object到字節數組的轉換 */ @Test public void testObject2ByteArray() throws IOException, ClassNotFoundException { // Object obj = ""; Integer[] obj = { 1 , 3 , 4 }; // // object to bytearray ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(obj); byte [] bytes = bo.toByteArray(); bo.close(); oo.close(); System.out.println(Arrays.toString(bytes)); Integer[] intArr = (Integer[]) testByteArray2Object(bytes); System.out.println(Arrays.asList(intArr)); byte [] b2 = intToByte( 123 ); System.out.println(Arrays.toString(b2)); int a = byteToInt(b2); System.out.println(a); } /** * 字節數組到object的轉換. */ private Object testByteArray2Object( byte [] bytes) throws IOException, ClassNotFoundException { // byte[] bytes = null; Object obj; // bytearray to object ByteArrayInputStream bi = new ByteArrayInputStream(bytes); ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); bi.close(); oi.close(); System.out.println(obj); return obj; } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!