自己写了一个,不知道有没有简单的方法 if(hexString.length()%2 != 0) throw new Exception("Hex字符串长度不正确"); int length = hexString.length()/2; byte[] newBytes = new byte[length]; for(int i=0;i<length;i++) { String str = hexString.substring(i*2,(i+1)*2); newBytes[i] = (byte)Integer.parseInt(str,16); }
return newBytes;
public class Test { public static final int BytesToInt(int b[]) throws Exception { if (b.length != 4) throw new Exception("invalid length"); int result = 0; for (int i = 0; i < 4; i++) { result = ( (result << 4) | Integer.parseInt(String.valueOf(b[i]),2)); } return result; } public static void main(String args[]) { try { int[] val = {0, 00, 0001, 1001}; System.out.println(Test.BytesToInt(val)); } catch (Exception ex) { } } }
没有api级的
byte[0] = 10101011
byte[1] = 10111010
if(hexString.length()%2 != 0)
throw new Exception("Hex字符串长度不正确");
int length = hexString.length()/2;
byte[] newBytes = new byte[length];
for(int i=0;i<length;i++)
{
String str = hexString.substring(i*2,(i+1)*2);
newBytes[i] = (byte)Integer.parseInt(str,16);
}
return newBytes;
if (b.length != 4)
throw new Exception("invalid length");
int result = 0;
for (int i = 0; i < 4; i++) {
result = ( (result << 4) | Integer.parseInt(String.valueOf(b[i]),2));
}
return result;
}
public static void main(String args[]) {
try {
int[] val = {0, 00, 0001, 1001};
System.out.println(Test.BytesToInt(val));
}
catch (Exception ex) {
}
}
}