将MAC地址字节数组格式化为String

我正在使用此代码来查找计算机的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(); } 

Mac地址没有标准的文本表示。 您只需将其转换为hex并将字节分开以便于阅读。 这是我在Unix上使用ifconfig格式的函数,

 public static String getMacAddress(String ipAddr) throws UnknownHostException, SocketException { InetAddress addr = InetAddress.getByName(ipAddr); NetworkInterface ni = NetworkInterface.getByInetAddress(addr); if (ni == null) return null; byte[] mac = ni.getHardwareAddress(); if (mac == null) return null; StringBuilder sb = new StringBuilder(18); for (byte b : mac) { if (sb.length() > 0) sb.append(':'); sb.append(String.format("%02x", b)); } return sb.toString(); } 

您只需将’:’更改为’ – ‘即可。

通过此,您可以轻松地格式化Mac地址字符串。

 import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class App{ public static void main(String[] args){ InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); NetworkInterface network = NetworkInterface.getByInetAddress(ip); byte[] mac = network.getHardwareAddress(); System.out.print("Current MAC address : "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } System.out.println(sb.toString()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e){ e.printStackTrace(); } } } 

从这里复制: http : //www.mkyong.com/java/how-to-get-mac-address-in-java/comment-page-1/#comment-139182

也许你可以使用commons-codec的Hex.encodeHex(bytes)

以下是其他方法,没有第三方库。

应该是这样的

 StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length(); i++) { b.append(String.format("%02X%s", mac[i], (i < mac.length() - 1) ? "-" : ""); String s = sb.toString(); 
  private static final byte[] NULL_MAC = new byte[] {0, 0, 0, 0, 0, 0}; public static String getMacString(byte[] macAddress) { StringBuilder retval = new StringBuilder(17); if (macAddress == null) { macAddress = NULL_MAC; } boolean isFirst = true; for (byte b : macAddress) { if (!isFirst) { retval.append(":"); } else { isFirst = false; } retval.append(String.format("%02x", b & 0xff)); } return retval.toString(); } 

我知道这是一个与Java相关的问题,但对于像我一样在这里结束的Scala用户,这是一种在Scala中执行此操作的方法:

 bytes.map("%02X" format _).mkString (":") 
 String s=""; for (int i = 0; i < mac.length; i++) { s += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""); }