1.byte16进制数转换10进制
byte[] b = new byte[]{(byte)Integer.parseInt("af", 16),(byte)Integer.parseInt("03", 16),
(byte)Integer.parseInt("f2", 16),(byte)Integer.parseInt("03", 16)};
int s = 0;
s = (b[0] & 0xff);
System.out.println(s);
2.字符串16进制数组转换int10进制数组public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String[] number = new String[] { "01", "02", "04", "0f", "ff", "af" };
int[] con = new Test().convert(number);
for (int i : con) {
System.out.print(i + ",");
}
}
private int[] convert(String[] num) {
int[] number = new int[num.length];
for (int z = 0; z < num.length; z++) {
String str = num[z];
String myStr[] = { "a", "b", "c", "d", "e", "f" };
int result = 0;
int n = 1;
for (int i = str.length() - 1; i >= 0; i--) {
String param = str.substring(i, i + 1);
for (int j = 0; j < myStr.length; j++) {
if (param.equalsIgnoreCase(myStr[j])) {
param = "1" + String.valueOf(j);
}
}
result += Integer.parseInt(param) * n;
n *= 16;
}
number[z] = result;
}
return number;
}
}
3.java二进制,字节数组,字符,十六进制,BCD编码转换
// 整数到字节数组转换
public static byte[] int2bytes(int n) {
byte[] ab = new byte[4];
ab[0] = (byte) (0xff & n);
ab[1] = (byte) ((0xff00 & n) >> 8);
ab[2] = (byte) ((0xff0000 & n) >> 16);
ab[3] = (byte) ((0xff000000 & n) >> 24);
return ab;
}
// 字节数组到整数的转换
public static int bytes2int(byte b[]) {
int s = 0;
s = ((((b[0] & 0xff) << 8 | (b[1] & 0xff)) << 8) | (b[2] & 0xff)) << 8
| (b[3] & 0xff);
return s;
}
// 字节转换到字符
public static char byte2char(byte b) {
return (char) b;
}
private final static byte[] hex = "0123456789ABCDEF".getBytes();
private static int parse(char c) {
if (c >= 'a')
return (c - 'a' + 10) & 0x0f;
if (c >= 'A')
return (c - 'A' + 10) & 0x0f;
return (c - '0') & 0x0f;
}
// 从字节数组到十六进制字符串转换
public static String Bytes2HexString(byte[] b) {
byte[] buff = new byte[2 * b.length];
for (int i = 0; i < b.length; i++) {
buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
buff[2 * i + 1] = hex[b[i] & 0x0f];
}
return new String(buff);
}
// 从十六进制字符串到字节数组转换
public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
}