80,351
社区成员
发帖
与我相关
我的任务
分享
byte[] send_to_pc_buf = new byte[100];
String ab ="0012234567EA5E7D45BFFA";
int j = 0;
for(int i=0;i<ab.length();i=i+2){
send_to_pc_buf[j] = (byte) Integer.parseInt(ab.substring(i,i+2) ,16);
j++;
}
send_to_pc_str = new String(send_to_pc_buf);
public int hexToInt(byte b) throws Exception {
if (b >= '0' && b <= '9') {
return (int)b - '0';
}
if (b >= 'a' && b <= 'f') {
return (int)b + 10 - 'a';
}
if (b >= 'A' && b <= 'F') {
return (int)b + 10 - 'A';
}
throw new Exception("invalid hex");
}
public byte[] decodeToBytes(String hexString) {
byte[] hex = hexString.getBytes();
if ((hex.length % 2) != 0) {
return null;
}
byte[] ret = new byte[hex.length / 2];
int j = 0;
int i = 0;
try {
while (i < hex.length) {
byte hi = hex[i++];
byte lo = hex[i++];
ret[j++] = (byte)((hexToInt(hi) << 4) | hexToInt(lo));
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return ret;
}
public String decodeToString(String hexString) {
return new String(decodeToBytes(hexString));
}