這兩天寫Android遇到了進制的編解碼問題,記錄一下Byte數(shù)組與十六進制互轉(zhuǎn)。
Byte數(shù)組轉(zhuǎn)十六進制
public static String byte2HexString(byte[] bytes) {
String hex = "";
if (bytes != null) {
for (Byte b : bytes) {
hex += String.format("%02X", b.intValue() & 0xFF);
}
}
return hex;
}
十六進制轉(zhuǎn)Byte數(shù)組
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
try {
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
} catch (Exception e) {
// Log.d("", "Argument(s) for hexStringToByteArray(String s)"+ "was not a hex string");
}
return data;
}