/**
- 01.计价器 16进制SCII转10进制字符串
- 例如:ASCII :BEA94A4C313233343536 转成字符串 京JL123456
- @param value 要转化的ASCII
- @return 10进制字符串
*/
public static String asciiToString(String value) {
try {
return new String(hex2byte(change(value)), “UTF-8”);
} catch (UnsupportedEncodingException e) {
return value;
}
}
public static byte[] hex2byte(String value) {
String[] str = value.split(" ");
byte[] b = new byte[str.length];
for (int i = 0; i < str.length; i++) {
b[i] = (byte) hexStringToInt(str[i]);
}
return b;
}
/**
- 数据解析 把16进制转换出来 038e转换成 1000
- @param value
- @return
*/
public static int hexStringToInt(String value) {
return Integer.valueOf(value, 16);
}
/**
- 02.计价器 十六进制字符串转Byte数组
- @param value 十六进制字符串
- @return Byte数组
*/
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;
}
/**
- 03.计价器 Byte数组转十六进制字符串
- @param value Byte数组
- @return 十六进制字符串
*/
public static String byte2HexString(byte[] bytes) {
String hex= “”;
if (bytes != null) {
for (Byte b : bytes) {
hex += String.format("%02X", b.intValue() & 0xFF);
}
}
return hex;
}