本文部分代碼摘錄自網(wǎng)上,并稍加整理,用于字節(jié)與十六進(jìn)制之間的轉(zhuǎn)換。
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
|
/** * reference apache commons <a * href="http://commons.apache.org/codec/">http://commons.apache.org/codec/</a> * * byte占用8位,十六進(jìn)制字符占用4位。所以可以把一個(gè)byte轉(zhuǎn)換成兩個(gè)相應(yīng)的十六進(jìn)制字符,即把byte的高4位和低4位 * 分別轉(zhuǎn)換成相應(yīng)的十六進(jìn)制字符H和L,并組合起來。相反的轉(zhuǎn)換也是同理。 * */ public class Hex { /** * 用于建立十六進(jìn)制字符的輸出 */ private static final char [] DIGITS_LOWER = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' }; /** * 用于建立十六進(jìn)制字符的輸出 */ private static final char [] DIGITS_UPPER = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' }; /** * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組。 * * 因?yàn)槭褂脙蓚€(gè)字符表示一個(gè)字節(jié),所以返回的char[]長度將是參數(shù)byte[]長度的兩倍。 * * @param data * 用于轉(zhuǎn)換為十六進(jìn)制字符的byte[] * @return 包含十六進(jìn)制字符的char[] */ public static char [] encodeHex( final byte [] data) { return encodeHex(data, true ); } /** * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組。 * * 因?yàn)槭褂脙蓚€(gè)字符表示一個(gè)字節(jié),所以返回的char[]長度將是參數(shù)byte[]長度的兩倍。 * * @param data * 用于轉(zhuǎn)換為十六進(jìn)制字符的byte[] * @param toLowerCase * <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式 * @return 包含十六進(jìn)制字符的char[] */ public static char [] encodeHex( final byte [] data, final boolean toLowerCase) { return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } /** * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組。 * * 因?yàn)槭褂脙蓚€(gè)字符表示一個(gè)字節(jié),所以返回的char[]長度將是參數(shù)byte[]長度的兩倍。 * * @param data * 用于轉(zhuǎn)換為十六進(jìn)制字符的byte[] * @param toDigits * 用于控制輸出的字母表 * @return 包含十六進(jìn)制字符的char[] */ protected static char [] encodeHex( final byte [] data, final 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; } /** * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串。 * * 因?yàn)槭褂脙蓚€(gè)字符表示一個(gè)字節(jié),所以返回的的字符串長度將是參數(shù)byte[]長度的兩倍。 * * @param data * 用于轉(zhuǎn)換為十六進(jìn)制字符的byte[] * @return 十六進(jìn)制字符串 */ public static String encodeHexStr( final byte [] data) { return encodeHexStr(data, true ); } /** * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串。 * * 因?yàn)槭褂脙蓚€(gè)字符表示一個(gè)字節(jié),所以返回的的字符串長度將是參數(shù)byte[]長度的兩倍。 * * @param data * 用于轉(zhuǎn)換為十六進(jìn)制字符的byte[] * @param toLowerCase * <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式 * @return 十六進(jìn)制字符串 */ public static String encodeHexStr( byte [] data, boolean toLowerCase) { return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } /** * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串。 * * 因?yàn)槭褂脙蓚€(gè)字符表示一個(gè)字節(jié),所以返回的的字符串長度將是參數(shù)byte[]長度的兩倍。 * * @param data * 用于轉(zhuǎn)換為十六進(jìn)制字符的byte[] * @param toDigits * 用于控制輸出的字母表 * @return 十六進(jìn)制字符串 */ protected static String encodeHexStr( byte [] data, char [] toDigits) { return new String(encodeHex(data, toDigits)); } /** * 將十六進(jìn)制字符數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組 * * @param data * 十六進(jìn)制char[] * @return byte[] * @throws RuntimeException * 如果源十六進(jìn)制字符數(shù)組的長度是奇數(shù),將拋出運(yùn)行時(shí)異常 */ public static byte [] decodeHex( char [] data) { int len = data.length; if ((len & 0x01 ) != 0 ) { throw new RuntimeException( "Odd number of characters." ); } // 一個(gè)byte對應(yīng)兩個(gè)十六進(jìn)制字符,則將byte[]大小設(shè)置為char[]大小的一半 byte [] out = new byte [len >> 1 ]; // two characters form the hex value. for ( int i = 0 , j = 0 ; j < len; i++) { int f = toDigit(data[j], j) << 4 ; j++; f = f | toDigit(data[j], j); j++; out[i] = ( byte ) (f & 0xFF ); } return out; } /** * 將十六進(jìn)制字符轉(zhuǎn)換成一個(gè)整數(shù)。 * * @param ch * 要轉(zhuǎn)換成整數(shù)的字符 * @param index * 字符在字符數(shù)組中的位置 * @return 一個(gè)整數(shù) * @throws RuntimeException * 當(dāng)ch不是一個(gè)合法的十六進(jìn)制字符時(shí),拋出該異常 */ protected static int toDigit( final char ch, final int index) { final int digit = Character.digit(ch, 16 ); if (digit == - 1 ) { throw new RuntimeException( "Illegal hexadecimal character " + ch + " at index " + index); } return digit; } public static void main(String[] args) { String srcStr = "HelloWorld!" ; String encodeStr = encodeHexStr(srcStr.getBytes(), false ); String decodeStr = new String(decodeHex(encodeStr.toCharArray())); System.out.println( "源字符串:" + srcStr); System.out.println( "字符串編碼為十六進(jìn)制:" + encodeStr); System.out.println( "十六進(jìn)制解碼為字符串:" + decodeStr); } } |