如何将Mac地址转换为十六进制并将其传递给java中的bytearray [英] How to convert a Mac Address to a Hex and pass it to a bytearray in java

查看:151
本文介绍了如何将Mac地址转换为十六进制并将其传递给java中的bytearray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将MacAddress转换为十六进制字符串,然后将其解析为java中的一个字节?
和类似的IP地址?

谢谢

解决方案

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

  String macAddress =AA:BB:CC:DD:EE:FF; 
String [] macAddressParts = macAddress.split(:);

//将十六进制字符串转换为字节值
Byte [] macAddressBytes = new Byte [6];
for(int i = 0; i <6; i ++){
Integer hex = Integer.parseInt(macAddressParts [i],16);
macAddressBytes [i] = hex.byteValue();
}

还有......

 字符串ipAddress =192.168.1.1; 
String [] ipAddressParts = ipAddress.split(\\\。);

//将int字符串转换为字节值
Byte [] ipAddressBytes = new Byte [4];
for(int i = 0; i <4; i ++){
Integer integer = Integer.parseInt(ipAddressParts [i]);
ipAddressBytes [i] = integer.byteValue();
}


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?

Thank you

解决方案

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

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

这篇关于如何将Mac地址转换为十六进制并将其传递给java中的bytearray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆