将字节数组转换为IPAddress

时间:2011-01-19 19:33:10

标签: .net ip-address

.Net IPAddress类GetAddressBytes方法可用于将IPAddress转换为字节数组。

我的问题是我有这些字节数组,如何将它们转换回IPAddress对象,或IP字符串? (重要的是要有一个适用于IPv4和IPv6的解决方案)。

3 个答案:

答案 0 :(得分:7)

答案 1 :(得分:1)

什么andrey说

    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)

答案 2 :(得分:0)

@ dbasnett在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());