2010-05-09 86 views
7

我正在使用此代碼來查找計算機的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(); 
} 
+1

如果您需要以相同的方式返回格式化的字符串,請使用StringBuilder,並將其附加到以String.format(..)格式設置的循環部分中。 – 2010-05-09 11:09:44

+0

見http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa-in-c/632920#632920 – Zaki 2010-05-09 11:11:11

回答

18

Mac地址沒有標準文本表示。您只需要將其轉換爲十六進制,並將字節分隔以提高可讀性。下面是我在使用ifconfig的Unix上的格式使用你只需要改變的功能,

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

「:」到「 - 」。

+0

這看起來不錯我。如果性能受到關注,也可以避免使用String.format(),因爲它是一個衆所周知的慢函數。 – Chris 2011-05-20 17:46:23

+0

它給了我NullPointerException線'NetworkInterface ni = NetworkInterface.getByInetAddress(addr);'但我的InetAddress對象包含有效的IP地址...任何幫助表示讚賞.... – Durrat 2013-03-22 08:56:15

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

它應該是這樣的

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(); 
-1
String s=""; 
for (int i = 0; i < mac.length; i++) { 
    s += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""); 
} 
+1

與+ =串聯字符串是非常低效的(使用StringBuilder而不是像在其他答案中那樣)。另外,看起來你已經將過多的代碼捆綁到一條線上,既沒有幫助也沒有阻礙性能,但是當擴展到數千行時,會產生不可讀的代碼。 – Chris 2011-05-20 17:43:38

4

通過這一點,你可以很容易地甲的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

0

我知道這是一個Java相關的問題,但對於誰在這裏結束了像我一樣的Scala用戶,這是一個辦法做到這一點在斯卡拉:

bytes.map("%02X" format _).mkString (":") 
相關問題