Convert MAC address String to Byte Array

Total Views : 97
Zoom In Zoom Out Read Later Print

This code will help you to know how you can convert or format MAC address string into byte array.

MacStringToByteArray.java

package com.prodevsblog.how_to.network.ip;

public class MacStringToByteArray {

    public static void main(String[] args){
        // Used Split based on part divider
        //String macAddress = "3C:A9:F4:35:86:CC";
        //String[] macAddressParts = macAddress.split(":");

        String macAddress = "3C-A9-F4-35-86-CC";
        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();
        }

        for(Object obj : macAddressBytes ) {
            System.out.print(obj + " ");
        }
    }
}

Output of the code.

Output:

60 -87 -12 53 -122 -52 
Process finished with exit code 0

See More

Latest Photos