返回顶部

收藏

C# ip地址和整数之间的相互转换代码

更多
using System;
using System.Net;

class App
{
    static long ToInt(string addr)
    {
        // careful of sign extension: convert to uint first;
        // unsigned NetworkToHostOrder ought to be provided.
        return (long) (uint) IPAddress.NetworkToHostOrder(
             (int) IPAddress.Parse(addr).Address);
    }

    static string ToAddr(long address)
    {
        return IPAddress.Parse(address.ToString()).ToString();
        // This also works:
        // return new IPAddress((uint) IPAddress.HostToNetworkOrder(
        //    (int) address)).ToString();
    }

    static void Main()
    {
        //假设www.outofmemory.cn服务器ip地址为:64.233.187.99,换后的结果为1089059683
        Console.WriteLine(ToInt("64.233.187.99"));
        //1089059683转换后的结果为:64.233.187.99
        Console.WriteLine(ToAddr(1089059683));

    }
}

标签:ip转换,ip转整数,ip地址

收藏

0人收藏

支持

0

反对

0

发表评论