I have code for print MAC address from Java, the code is
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
but I has curiosity about this, and I'm trying to print directly with
System.out.print("Current MAC address : ");
for (int i = 0; i < mac.length; i++) {
System.out.print(mac[i]);
if (i < mac.length - 1)
System.out.print("-");
else
System.out.print("");
}
but it doesn't work.
The results are
Current MAC address : 08-00-27-96-40-39
Current MAC address : 8-0-39--106-64-57
why?
Thank's in advance for your help!!