将Mac地址和IP地址转换为特定大小的字节araay

时间:2019-05-16 17:10:21

标签: arrays vb.net byte ip-address mac-address

我正在尝试将mac地址转换为大小为5的字节数组,并将IP地址转换为大小为4的字节数组。现在,我知道如何使用以下代码来检索值:

'This returns a byte array of size 16
Dim ipArray As Byte() = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(Function(a As IPAddress) Not a.IsIPv6LinkLocal AndAlso Not a.IsIPv6Multicast AndAlso Not a.IsIPv6SiteLocal).First().GetAddressBytes

'This returns a byte array of size 6
Dim macArray As Byte() = NetworkInterface.GetAllNetworkInterfaces(0).GetPhysicalAddress.GetAddressBytes

这不能给我我想要的结果,也找不到适合的解决方案。对于IP,我创建了一种解决方法,但发现它很脏:

Dim ip As String = "192.168.0.10"
Dim _ip() As String = ip.Split(CChar("."))
Object.IpAddress = New Byte() {Byte.Parse(_ip(0)), Byte.Parse(_ip(1)), Byte.Parse(_ip(2)), Byte.Parse(_ip(3))}

1 个答案:

答案 0 :(得分:1)

尝试:

Dim ipArray As Byte() = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(Function(a As IPAddress) a.AddressFamily =2).First().GetAddressBytes