Formatting a byte array to string in java
        Posted  
        
            by rgksugan
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by rgksugan
        
        
        
        Published on 2010-05-09T11:04:43Z
        Indexed on 
            2010/05/09
            11:08 UTC
        
        
        Read the original article
        Hit count: 285
        
I am using this code to find the MAC address of a machine.This code prints directly the MAC address, but i want to return it as a string.I am completely confused.
please help.
try {
        InetAddress add = InetAddress.getByName("10.123.96.102");
        NetworkInterface ni1 = NetworkInterface.getByInetAddress(add);
        if (ni1 != null) {
            byte[] mac1 = ni1.getHardwareAddress();
            if (mac1 != null) {
                for (int k = 0; k < mac1.length; k++) {
                    System.out.format("%02X%s", mac1[k], (k < mac1.length - 1) ? "-" : "");
                }
            } else {
                System.out.println("Address doesn't exist ");
            }
            System.out.println();
        } else {
            System.out.println("address is not found.");
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (SocketException e) {
        e.printStackTrace();
    }
© Stack Overflow or respective owner