解析十六进制用
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class HexUtil {
public static byte hexToByte(String inHex) {
return (byte) Integer.parseInt(inHex, 16);
}
public static String byteToHex(byte b) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() < 2) {
hex = "0" + hex;
}
return hex;
}
public static String bytesToHex(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() < 2) {
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
public static int byteToInt(byte b) {
return b & 0xFF;
}
public static int bytesToInt(byte[] ary) {
int value;
value = (int) ((ary[0] & 0xFF) | ((ary[1] << 8) & 0xFF00));
return value;
}
public static byte[] hexToByteArray(String inHex) {
int hexlen = inHex.length();
byte[] result;
if (hexlen % 2 == 1) {
hexlen++;
result = new byte[(hexlen / 2)];
inHex = "0" + inHex;
} else {
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2) {
result[j] = hexToByte(inHex.substring(i, i + 2));
j++;
}
return result;
}
public static String bytesToUTF8(byte[] bytes) throws UnsupportedEncodingException {
String str = new String(bytes, "UTF-8");
return str;
}
public static String bytesSumHex(byte[] bytes) {
long sum = 0;
for (byte b : bytes) {
sum += b;
}
return Long.toHexString(sum);
}
public static byte[] hexStringToHighLowConversionTwoBytes(String str) {
byte[] result = new byte[2];
byte[] bytes = hexToByteArray(str);
result[0] = bytes[bytes.length - 1];
if (bytes.length >= 2) {
result[1] = bytes[bytes.length - 2];
} else {
result[1] = 0x00;
}
return result;
}}