android 常用工具类。字符串转数组。数组转字符串。
HexUtil.java
/**
* 字符串转字节工具类
*/
public class HexUtil {
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static String bytesToHexString(byte[] b) {
if (b.length == 0) {
return null;
}
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < b.length; i++) {
int value = b[i] & 0xFF;
String hv = Integer.toHexString(value);
if (hv.length() < 2) {
sb.append(0);
}
sb.append(hv);
}
return sb.toString();
}
}