问题
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();
}
回答1:
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,
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();
}
You just need to change the ':' to '-'.
回答2:
By this you can easily formate Mac Address String.
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();
}
}
}
copy from here : http://www.mkyong.com/java/how-to-get-mac-address-in-java/comment-page-1/#comment-139182
回答3:
Perhaps you could use Hex.encodeHex(bytes) from commons-codec.
Here are other ways to do this, without 3rd party libraries.
回答4:
It should be something like
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();
回答5:
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();
}
回答6:
String s="";
for (int i = 0; i < mac.length; i++) {
s += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
回答7:
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:
bytes.map("%02X" format _).mkString (":")
回答8:
For something lightweight and fast, try the following. 3rd party external dependencies are minimal and just uses some "old school" bit math.
public static String buildMACAddressString(byte[] macaddress) {
char[] buffer = new char[macaddress.length*3];
char[] inttohex= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
int destIndex=0;
byte byteValue;
for (int i = 0; i < macaddress.length; i++) {
// pull current byte value
byteValue = (byte) (macaddress[i] & 0xff);
// convert high nibble to hex char and store into char array..
buffer[destIndex++]=inttohex[(byteValue&0xf0)>>4];
// Convert low nibble to hex char and store into char array..
buffer[destIndex++]=inttohex[byteValue&0xf];
// Inject spacer
if (i < macaddress.length-1)
buffer[destIndex++]=':';
}
return String.valueOf(buffer,0,destIndex);
}
来源:https://stackoverflow.com/questions/2797430/formatting-mac-address-byte-array-to-string