在许多编程语言中,您可以使用内置的库或函数将字符串形式的MAC地址转换为字节数组
def mac_to_bytes(mac_str):
return bytes.fromhex(mac_str.replace(':', ''))
mac_address = "00:1A:2B:3C:4D:5E"
byte_array = mac_to_bytes(mac_address)
print(byte_array)
public static byte[] macToBytes(String macStr) {
String[] hex = macStr.split(":");
byte[] bytes = new byte[6];
for (int i = 0; i < hex.length; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
}
return bytes;
}
String macAddress = "00:1A:2B:3C:4D:5E";
byte[] byteArray = macToBytes(macAddress);
System.out.println(Arrays.toString(byteArray));
using System;
using System.Linq;
public static byte[] MacToBytes(string macStr)
{
return macStr.Split(':').Select(s => Convert.ToByte(s, 16)).ToArray();
}
string macAddress = "00:1A:2B:3C:4D:5E";
byte[] byteArray = MacToBytes(macAddress);
Console.WriteLine(string.Join(", ", byteArray));
这些示例中的函数都接受一个字符串形式的MAC地址(如 "00:1A:2B:3C:4D:5E"),并将其转换为一个字节数组。
领取专属 10元无门槛券
手把手带您无忧上云