MAC 转 Byte[] 数组

/**
* MAC 地址转 byte[]
* 默认以小端序转换
*
* @param macAddr "E4:54:E8:81:FC:FD"
* @return byte数组
*/
public static byte[] macToBytes(String macAddr) {
String[] hex = macAddr.split(":");
if (macAddr.contains("-")) {
hex = macAddr.split("-");
}
if (hex.length != 6) {
//TODO throw new Exception("MAC 地址不正确");
}
byte[] returnBytes = new byte[6];
for (int i = 0; i < hex.length; i++) {
//parseInt()方法用于将字符串参数作为有符号的n进制整数进行解析
int value = Integer.parseInt(hex[i], 16);
returnBytes[i] = (byte) value;
if (i >= 5) {
//如果超过6位,就跳过,容错
break;
}
}
return returnBytes;
}