Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting MAC address byte array to String

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();
}
like image 632
rgksugan Avatar asked May 09 '10 11:05

rgksugan


People also ask

Can byte array be converted to string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

Is byte array a string?

Remember that a string is basically just a byte array For example, the following code iterates over every byte in a string and prints it out as both a string and as a byte.

Can we convert byte array to file in Java?

Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.


4 Answers

There is no standard text representation for Mac addresses. You just need to convert it to hex and separate the bytes for readability. Here is the function I use in the format of ifconfig on Unix,

public static String getMacAddress(String ipAddr)
        throws UnknownHostException, SocketException {
    InetAddress addr = InetAddress.getByName(ipAddr);
    NetworkInterface ni = NetworkInterface.getByInetAddress(addr);
    if (ni == null)
        return null;

    byte[] mac = ni.getHardwareAddress();
    if (mac == null)
        return null;

    StringBuilder sb = new StringBuilder(18);
    for (byte b : mac) {
        if (sb.length() > 0)
            sb.append(':');
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

You just need to change the ':' to '-'.

like image 58
ZZ Coder Avatar answered Sep 28 '22 22:09

ZZ Coder


By this you can easily formate Mac Address String.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class App{

   public static void main(String[] args){

    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();

    }

   }

}

copy from here : http://www.mkyong.com/java/how-to-get-mac-address-in-java/comment-page-1/#comment-139182

like image 32
Az MaYo Avatar answered Sep 28 '22 22:09

Az MaYo


Perhaps you could use Hex.encodeHex(bytes) from commons-codec.

Here are other ways to do this, without 3rd party libraries.

like image 31
Bozho Avatar answered Sep 28 '22 23:09

Bozho


It should be something like

StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length(); i++) {
    b.append(String.format("%02X%s", mac[i], (i < mac.length() - 1) ? "-" : "");

String s = sb.toString();
like image 45
dude Avatar answered Sep 28 '22 22:09

dude