01.
// hex to byte[]
02.
public static byte[] hexToByteArray(String hex)
03.
{
04.
if
(hex ==
null
|| hex.
length
() == 0)
05.
return
null
;
06.
07.
byte[] ba =
new
byte[hex.
length
() / 2];
08.
for
(int i = 0; i < ba.
length
; i++)
09.
ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
10.
11.
return
ba;
12.
}
13.
14.
// byte[] to hex
15.
public static String byteArrayToHex(byte[] ba)
16.
{
17.
if
(ba ==
null
|| ba.
length
== 0)
18.
return
null
;
19.
20.
21.
StringBuffer sb =
new
StringBuffer(ba.
length
* 2);
22.
String hexNumber;
23.
for
(int x = 0; x < ba.
length
; x++)
24.
{
25.
hexNumber =
"0"
+ Integer.toHexString(0xff & ba[x]);
26.
sb.
append
(hexNumber.substring(hexNumber.
length
() - 2));
27.
}
28.
return
sb.toString();
29.
}
全站熱搜
留言列表