如何将 Mac 地址转换为十六进制并将其传递给 java 中的字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10835773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:47:09  来源:igfitidea点击:

How to convert a Mac Address to a Hex and pass it to a bytearray in java

javaandroidhex

提问by Mr.Noob

How can i convert a MacAddress to a Hex String and then parse it to a byte in java? and similarly an IP Address as well?

如何将 MacAddress 转换为十六进制字符串,然后在 Java 中将其解析为字节?以及类似的 IP 地址?

Thank you

谢谢

回答by Ben Holland

A MAC address is already in hexadecimal format, it is of the form of 6 pairs of 2 hexadecimal digits.

MAC地址已经是16进制格式了,是6对2个16进制数字的形式。

String macAddress = "AA:BB:CC:DD:EE:FF";
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();
}

And...

和...

String ipAddress = "192.168.1.1";
String[] ipAddressParts = ipAddress.split("\.");

// convert int string to byte values
Byte[] ipAddressBytes = new Byte[4];
for(int i=0; i<4; i++){
    Integer integer = Integer.parseInt(ipAddressParts[i]);
    ipAddressBytes[i] = integer.byteValue();
}

回答by Sean F

The open-source IPAddress Java librarywill parse both MAC addresses and IP addresses, and convert to bytes in a polymorphic manner. Disclaimer: I am the project manager of that library.

开源的 IPAddress Java 库将解析 MAC 地址和 IP 地址,并以多态方式转换为字节。免责声明:我是那个图书馆的项目经理。

The following code will do what you are requesting:

以下代码将执行您的要求:

    String ipv4Addr = "1.2.3.4";
    String ipv6Addr = "aaaa:bbbb:cccc:dddd::";
    String macAddr = "a:b:c:d:e:f";
    try {
        HostIdentifierString addressStrings[] = {
            new IPAddressString(ipv4Addr),
            new IPAddressString(ipv6Addr),
            new MACAddressString(macAddr)
        };
        Address addresses[] = new Address[addressStrings.length];
        for(int i = 0; i < addressStrings.length; i++) {
            addresses[i] = addressStrings[i].toAddress();//parse
        }
        for(Address addr : addresses) {
            byte bytes[] = addr.getBytes();
            //convert to a list of positive Integer for printing
            List<Integer> forPrinting = IntStream.range(0, bytes.length).map(idx -> 0xff & bytes[idx]).boxed().collect(Collectors.toList());
            System.out.println("bytes for " + addr + " are " + forPrinting);
        }
    } catch(HostIdentifierException | IOException e) {
        //e.getMessage has validation error
    }

output:

输出:

    bytes for 1.2.3.4 are [1, 2, 3, 4]
    bytes for aaaa:bbbb:cccc:dddd:: are [170, 170, 187, 187, 204, 204, 221, 221, 0, 0, 0, 0, 0, 0, 0, 0]
    bytes for 0a:0b:0c:0d:0e:0f are [10, 11, 12, 13, 14, 15]