正如我上面所说,我使用Integer.parseInt将十六进制值转换为十进制,但是当我输入正十六进制值时,我一直得到返回的负整数:
byte[] bytes2 = getMacBytes("90:e6:ba:97:4a:bb");
private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
byte[] bytes = new byte[6];
String[] hex = macStr.split("(\\:|\\-)");
for (int i = 0; i < 6; i++){
System.out.println(hex[i]);
}
if (hex.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
try {
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
System.out.println(bytes[i]);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit in MAC address.");
}
return bytes;
} …Run Code Online (Sandbox Code Playgroud)