如何将 MacAddress 转换为十六进制字符串,然后将其解析为 java 中的字节?
以及类似的 IP 地址?
谢谢
MAC地址已经是十六进制格式,它是6对2个十六进制数字的形式。
String macAddress = "AA:BB:CC:DD:EE:FF";
String[] macAddressParts = macAddress.split(":");
// convert hex string to byte values
Byte[] macAddressBytes = new Byte[6];
for(int i=0; i<6; i++){
Integer hex = Integer.parseInt(macAddressParts[i], 16);
macAddressBytes[i] = hex.byteValue();
}
And...
String ipAddress = "192.168.1.1";
String[] ipAddressParts = ipAddress.split("\\.");
// convert int string to byte values
Byte[] ipAddressBytes = new Byte[4];
for(int i=0; i<4; i++){
Integer integer = Integer.parseInt(ipAddressParts[i]);
ipAddressBytes[i] = integer.byteValue();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)