How can i convert a MacAddress to a Hex String and then parse it to a byte in java? and similarly an IP Address as well?
Thank you
A MAC address is already in hexadecimal format, it is of the form of 6 pairs of 2 hexadecimal digits.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With