Reputation: 893
.Net IPAddress class GetAddressBytes method can be used to convert an IPAddress to an array of bytes.
My problem is one I have these array of bytes, how do I convert them back to an IPAddress object, or an IP string?? (Its important to have a solution working for both IPv4 and IPv6).
Upvotes: 3
Views: 13602
Reputation: 2311
@dbasnett's answer in C#
IPAddress ipv4Addr = IPAddress.Parse("224.0.0.1");
IPAddress ipv6Addr = IPAddress.Parse("ff00:0:0:0:0:0:e000:1");
Console.WriteLine(ipv4Addr.ToString());
Console.WriteLine(ipv6Addr.ToString());
byte[] b = ipv4Addr.GetAddressBytes();
ipv4Addr = new IPAddress(b);
b = ipv6Addr.GetAddressBytes();
ipv6Addr = new IPAddress(b);
Console.WriteLine(ipv4Addr.ToString());
Console.WriteLine(ipv6Addr.ToString());
Upvotes: 0
Reputation: 11773
What andrey said
Dim ipv4Addr As Net.IPAddress = Net.IPAddress.Parse("224.0.0.1")
Dim ipv6Addr As Net.IPAddress = Net.IPAddress.Parse("ff00:0:0:0:0:0:e000:1")
Debug.WriteLine(ipv4Addr.ToString)
Debug.WriteLine(ipv6Addr.ToString)
Dim b() As Byte = ipv4Addr.GetAddressBytes
ipv4Addr = New Net.IPAddress(b)
b = ipv6Addr.GetAddressBytes
ipv6Addr = New Net.IPAddress(b)
Debug.WriteLine(ipv4Addr.ToString)
Debug.WriteLine(ipv6Addr.ToString)
Upvotes: 1