Java Mac Address to String macToString(final byte[] address)

Here you can find the source of macToString(final byte[] address)

Description

Convert specified array of bytes into an mac address string.

License

Open Source License

Parameter

Parameter Description
address the address

Return

the mac address string

Declaration

public static String macToString(final byte[] address) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w  w w .  j  a v  a  2 s.c  om
     * Convert specified array of bytes into an mac address string.
     *
     * @param address the address
     * @return the mac address string
     */
    public static String macToString(final byte[] address) {
        final StringBuffer sb = new StringBuffer(address.length * 3);
        for (int i = 0; i < address.length; i++) {
            if (0 != i) {
                sb.append(':');
            }
            appendHexValue(sb, address[i]);
        }
        return sb.toString();
    }

    /**
     * Convert a byte into hex value and add to buffer.
     *
     * @param sb the StringBuffer.
     * @param data the data.
     */
    static void appendHexValue(final StringBuffer sb, final byte data) {
        sb.append(nibbleToHex((byte) (data >> 4)));
        sb.append(nibbleToHex(data));
    }

    /**
     * Convert lower 4 bits into a hex character.
     *
     * @param data the data byte
     * @return the hex character
     */
    static char nibbleToHex(final byte data) {
        final int nibble = data & 0xf;
        if (nibble <= 9) {
            return (char) ('0' + nibble);
        } else {
            return (char) ('A' + nibble - 10);
        }
    }
}

Related

  1. macToBinary(String mac)
  2. macToStr(byte[] mac)
  3. macToString(byte[] mac)
  4. macToString(byte[] mac)
  5. macToString(long macAddress)
  6. macToString(String srcstr)