将 MAC 地址字节数组格式化为字符串
我正在使用此代码来查找机器的 MAC 地址。此代码直接打印 MAC 地址,但我想将其作为字符串返回。我完全困惑了。
请帮忙。
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Mac 地址没有标准的文本表示形式。您只需将其转换为十六进制并分隔字节以提高可读性。这是我在 Unix 上以 ifconfig 格式使用的函数,
只需将 ':' 更改为 '-' 即可。
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,
You just need to change the ':' to '-'.
通过此您可以轻松格式化 Mac 地址字符串。
从这里复制: http://www.mkyong.com/java/how-to-get-mac-address-in-java/comment-page-1/#comment-139182
By this you can easily formate Mac Address String.
copy from here : http://www.mkyong.com/java/how-to-get-mac-address-in-java/comment-page-1/#comment-139182
也许你可以使用
Hex.encodeHex (字节)
来自 commons-codec。这里有其他方法可以做到这一点,无需第三方库。
Perhaps you could use
Hex.encodeHex(bytes)
from commons-codec.Here are other ways to do this, without 3rd party libraries.
应该是这样的
It should be something like
我知道这是一个与 Java 相关的问题,但对于像我一样最终来到这里的 Scala 用户,这是在 Scala 中执行此操作的一种方法:
I know this is a Java related question, but for Scala users who ended up here like I did, this is a way to do it in Scala:
对于轻量级和快速的东西,请尝试以下操作。第 3 方外部依赖很少,仅使用一些“老式”位数学。
For something lightweight and fast, try the following. 3rd party external dependencies are minimal and just uses some "old school" bit math.
有一种新方法,但似乎需要 Java 17。
您可以使用
java.util.HexFormat
类,专门定义一种格式(例如ofDelimiter(":")
,还有更多格式化的可能性),然后使用格式化程序(例如hexFormat.formatHex(ni.getHardwareAddress())
)。在上面的示例中,我有一个 IP 地址列表
List
,并使用它们来获取这些设备的 MAC 地址(如果可用)(if (ni.getHardwareAddress() != null) {...}
)。There is a new way, but it seems it requires Java 17.
You can use the
java.util.HexFormat
class, specifically define a format (e.g.ofDelimiter(":")
, there are many more formatting possibilities), and then use the formatter (e.g.hexFormat.formatHex(ni.getHardwareAddress())
).In the example above I have a list of IP addresses as
List<byte[]>
and use them to get the MAC addresses of those devices if available (if (ni.getHardwareAddress() != null) {...}
).Java 17+:
Java 17+:
科特林版本:
A kotlin version: