开发者

Convert an Array of Bytes to IPAddress

.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 开发者_StackOverflow中文版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).


You need IPAddress Constructor (Byte[])


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)


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜