本文實例講述了java實現的進制轉換工具類。分享給大家供大家參考,具體如下:
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
|
import java.nio.charset.charset; /** * 十六進制(簡寫為hex或下標16)在數學中是一種逢16進1的進位制,一般用數字0到9和字母a到f表示(其中:a~f即10~15)。<br> * 例如十進制數57,在二進制寫作111001,在16進制寫作39。<br> * 像java,c這樣的語言為了區(qū)分十六進制和十進制數值,會在十六進制數的前面加上 0x,比如0x20是十進制的32,而不是十進制的20<br> * * 參考:https://my.oschina.net/xinxingegeya/blog/287476 * * @author looly * */ public class hexkit { /** * 用于建立十六進制字符的輸出的小寫字符數組 */ private static final char [] digits_lower = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' }; /** * 用于建立十六進制字符的輸出的大寫字符數組 */ private static final char [] digits_upper = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' }; //---------------------------------------------------------------------------------------------------- encode /** * 將字節(jié)數組轉換為十六進制字符數組 * * @param data byte[] * @return 十六進制char[] */ public static char [] encodehex( byte [] data) { return encodehex(data, true ); } /** * 將字節(jié)數組轉換為十六進制字符數組 * * @param str 字符串 * @param charset 編碼 * @return 十六進制char[] */ public static char [] encodehex(string str, charset charset) { return encodehex(strkit.getbytes(str, charset), true ); } /** * 將字節(jié)數組轉換為十六進制字符數組 * * @param data byte[] * @param tolowercase <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式 * @return 十六進制char[] */ public static char [] encodehex( byte [] data, boolean tolowercase) { return encodehex(data, tolowercase ? digits_lower : digits_upper); } /** * 將字節(jié)數組轉換為十六進制字符串 * * @param data byte[] * @return 十六進制string */ public static string encodehexstr( byte [] data) { return encodehexstr(data, true ); } /** * 將字節(jié)數組轉換為十六進制字符串 * * @param data byte[] * @param tolowercase <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式 * @return 十六進制string */ public static string encodehexstr( byte [] data, boolean tolowercase) { return encodehexstr(data, tolowercase ? digits_lower : digits_upper); } //---------------------------------------------------------------------------------------------------- decode /** * 將十六進制字符數組轉換為字符串 * * @param hexstr 十六進制string * @param charset 編碼 * @return 字符串 */ public static string decodehexstr(string hexstr, charset charset) { if (strkit.isempty(hexstr)){ return hexstr; } return decodehexstr(hexstr.tochararray(), charset); } /** * 將十六進制字符數組轉換為字符串 * * @param hexdata 十六進制char[] * @param charset 編碼 * @return 字符串 */ public static string decodehexstr( char [] hexdata, charset charset) { return strkit.str(decodehex(hexdata), charset); } /** * 將十六進制字符數組轉換為字節(jié)數組 * * @param hexdata 十六進制char[] * @return byte[] * @throws runtimeexception 如果源十六進制字符數組是一個奇怪的長度,將拋出運行時異常 */ public static byte [] decodehex( char [] hexdata) { int len = hexdata.length; if ((len & 0x01 ) != 0 ) { throw new runtimeexception( "odd number of characters." ); } byte [] out = new byte [len >> 1 ]; // two characters form the hex value. for ( int i = 0 , j = 0 ; j < len; i++) { int f = todigit(hexdata[j], j) << 4 ; j++; f = f | todigit(hexdata[j], j); j++; out[i] = ( byte ) (f & 0xff ); } return out; } //---------------------------------------------------------------------------------------- private method start /** * 將字節(jié)數組轉換為十六進制字符串 * * @param data byte[] * @param todigits 用于控制輸出的char[] * @return 十六進制string */ private static string encodehexstr( byte [] data, char [] todigits) { return new string(encodehex(data, todigits)); } /** * 將字節(jié)數組轉換為十六進制字符數組 * * @param data byte[] * @param todigits 用于控制輸出的char[] * @return 十六進制char[] */ private static char [] encodehex( byte [] data, char [] todigits) { int l = data.length; char [] out = new char [l << 1 ]; // two characters form the hex value. for ( int i = 0 , j = 0 ; i < l; i++) { out[j++] = todigits[( 0xf0 & data[i]) >>> 4 ]; out[j++] = todigits[ 0x0f & data[i]]; } return out; } /** * 將十六進制字符轉換成一個整數 * * @param ch 十六進制char * @param index 十六進制字符在字符數組中的位置 * @return 一個整數 * @throws runtimeexception 當ch不是一個合法的十六進制字符時,拋出運行時異常 */ private static int todigit( char ch, int index) { int digit = character.digit(ch, 16 ); if (digit == - 1 ) { throw new runtimeexception( "illegal hexadecimal character " + ch + " at index " + index); } return digit; } //---------------------------------------------------------------------------------------- private method end /** * 2進制轉16進制 * @param bstring 2進制字符串 * @return */ public static string binary2hex(string bstring) { if (bstring == null || bstring.equals( "" ) || bstring.length() % 8 != 0 ) return null ; stringbuffer tmp = new stringbuffer(); int itmp = 0 ; for ( int i = 0 ; i < bstring.length(); i += 4 ) { itmp = 0 ; for ( int j = 0 ; j < 4 ; j++) { itmp += integer.parseint(bstring.substring(i + j, i + j + 1 )) << ( 4 - j - 1 ); } tmp.append(integer.tohexstring(itmp)); } return tmp.tostring(); } /** * 16進制轉2進制 * @param hexstring * @return */ public static string hex2binary(string hexstring) { if (hexstring == null || hexstring.length() % 2 != 0 ) return null ; string bstring = "" , tmp; for ( int i = 0 ; i < hexstring.length(); i++) { tmp = "0000" + integer.tobinarystring(integer.parseint(hexstring.substring(i, i + 1 ), 16 )); bstring += tmp.substring(tmp.length() - 4 ); } return bstring; } /** * 將二進制轉換成16進制 * @param buf * @return */ public static string binary2hex( byte buf[]) { stringbuffer sb = new stringbuffer(); for ( int i = 0 ; i < buf.length; i++) { string hex = integer.tohexstring(buf[i] & 0xff ); if (hex.length() == 1 ) { hex = '0' + hex; } sb.append(hex.touppercase()); } return sb.tostring(); } /** * 將16進制轉換為二進制 * @param hexstr * @return */ public static byte [] hex2byte(string hexstr) { if (hexstr.length() < 1 ) return null ; byte [] result = new byte [hexstr.length() / 2 ]; for ( int i = 0 ; i < hexstr.length() / 2 ; i++) { int high = integer.parseint(hexstr.substring(i * 2 , i * 2 + 1 ), 16 ); int low = integer.parseint(hexstr.substring(i * 2 + 1 , i * 2 + 2 ), 16 ); result[i] = ( byte ) (high * 16 + low); } return result; } } |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/huangbaokang/article/details/75034021