本文部分代碼摘錄自網上,并稍加整理,用于字節與十六進制之間的轉換。
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位,十六進制字符占用4位。所以可以把一個byte轉換成兩個相應的十六進制字符,即把byte的高4位和低4位 * 分別轉換成相應的十六進制字符H和L,并組合起來。相反的轉換也是同理。 * */ public class Hex { /** * 用于建立十六進制字符的輸出 */ 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' }; /** * 將字節數組轉換為十六進制字符數組。 * * 因為使用兩個字符表示一個字節,所以返回的char[]長度將是參數byte[]長度的兩倍。 * * @param data * 用于轉換為十六進制字符的byte[] * @return 包含十六進制字符的char[] */ public static char [] encodeHex( final byte [] data) { return encodeHex(data, true ); } /** * 將字節數組轉換為十六進制字符數組。 * * 因為使用兩個字符表示一個字節,所以返回的char[]長度將是參數byte[]長度的兩倍。 * * @param data * 用于轉換為十六進制字符的byte[] * @param toLowerCase * <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式 * @return 包含十六進制字符的char[] */ public static char [] encodeHex( final byte [] data, final boolean toLowerCase) { return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } /** * 將字節數組轉換為十六進制字符數組。 * * 因為使用兩個字符表示一個字節,所以返回的char[]長度將是參數byte[]長度的兩倍。 * * @param data * 用于轉換為十六進制字符的byte[] * @param toDigits * 用于控制輸出的字母表 * @return 包含十六進制字符的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; } /** * 將字節數組轉換為十六進制字符串。 * * 因為使用兩個字符表示一個字節,所以返回的的字符串長度將是參數byte[]長度的兩倍。 * * @param data * 用于轉換為十六進制字符的byte[] * @return 十六進制字符串 */ public static String encodeHexStr( final byte [] data) { return encodeHexStr(data, true ); } /** * 將字節數組轉換為十六進制字符串。 * * 因為使用兩個字符表示一個字節,所以返回的的字符串長度將是參數byte[]長度的兩倍。 * * @param data * 用于轉換為十六進制字符的byte[] * @param toLowerCase * <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式 * @return 十六進制字符串 */ public static String encodeHexStr( byte [] data, boolean toLowerCase) { return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } /** * 將字節數組轉換為十六進制字符串。 * * 因為使用兩個字符表示一個字節,所以返回的的字符串長度將是參數byte[]長度的兩倍。 * * @param data * 用于轉換為十六進制字符的byte[] * @param toDigits * 用于控制輸出的字母表 * @return 十六進制字符串 */ protected static String encodeHexStr( byte [] data, char [] toDigits) { return new String(encodeHex(data, toDigits)); } /** * 將十六進制字符數組轉換為字節數組 * * @param data * 十六進制char[] * @return byte[] * @throws RuntimeException * 如果源十六進制字符數組的長度是奇數,將拋出運行時異常 */ public static byte [] decodeHex( char [] data) { int len = data.length; if ((len & 0x01 ) != 0 ) { throw new RuntimeException( "Odd number of characters." ); } // 一個byte對應兩個十六進制字符,則將byte[]大小設置為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; } /** * 將十六進制字符轉換成一個整數。 * * @param ch * 要轉換成整數的字符 * @param index * 字符在字符數組中的位置 * @return 一個整數 * @throws RuntimeException * 當ch不是一個合法的十六進制字符時,拋出該異常 */ 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( "字符串編碼為十六進制:" + encodeStr); System.out.println( "十六進制解碼為字符串:" + decodeStr); } } |