How do you parse an IP address string to a uint value in C#?

后端 未结 9 732
庸人自扰
庸人自扰 2020-12-30 13:44

I\'m writing C# code that uses the windows IP Helper API. One of the functions I\'m trying to call is \"GetBestInterface\" that takes a \'uint\' representation of an IP. Wha

相关标签:
9条回答
  • 2020-12-30 14:01

    Complete solution:

    public static uint IpStringToUint(string ipString)
    {
        var ipAddress = IPAddress.Parse(ipString);
        var ipBytes = ipAddress.GetAddressBytes();
        var ip = (uint)ipBytes [0] << 24;
        ip += (uint)ipBytes [1] << 16;
        ip += (uint)ipBytes [2] <<8;
        ip += (uint)ipBytes [3];
        return ip;
    }
    
    public static string IpUintToString(uint ipUint)
    {
        var ipBytes = BitConverter.GetBytes(ipUint);
        var ipBytesRevert = new byte[4];
        ipBytesRevert[0] = ipBytes[3];
        ipBytesRevert[1] = ipBytes[2];
        ipBytesRevert[2] = ipBytes[1];
        ipBytesRevert[3] = ipBytes[0];
        return new IPAddress(ipBytesRevert).ToString();
    }
    

    Reverse order of bytes:

    public static uint IpStringToUint(string ipString)
    {
        return BitConverter.ToUInt32(IPAddress.Parse(ipString).GetAddressBytes(), 0);
    }
    
    public static string IpUintToString(uint ipUint)
    {
        return new IPAddress(BitConverter.GetBytes(ipUint)).ToString();
    }
    

    You can test here:

    https://www.browserling.com/tools/dec-to-ip

    http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx

    http://www.silisoftware.com/tools/ipconverter.php

    0 讨论(0)
  • 2020-12-30 14:05

    Byte arithmetic is discouraged, as it relies on all IPs being 4-octet ones.

    0 讨论(0)
  • 2020-12-30 14:06
    System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("192.168.1.1");
    
    byte[] bytes = ipAddress.GetAddressBytes();
    for (int i = 0; i < bytes.Length ; i++)
           Console.WriteLine(bytes[i]);
    

    Output will be 192 168 1 1

    0 讨论(0)
  • 2020-12-30 14:07

    Correct solution that observes Endianness:

    var ipBytes = ip.GetAddressBytes();
    ulong ip = 0;
    if (BitConverter.IsLittleEndian)
    {
        ip = (uint) ipBytes[0] << 24;
        ip += (uint) ipBytes[1] << 16;
        ip += (uint) ipBytes[2] << 8;
        ip += (uint) ipBytes[3];
    }
    else
    {
        ip = (uint)ipBytes [3] << 24;
        ip += (uint)ipBytes [2] << 16;
        ip += (uint)ipBytes [1] <<8;
        ip += (uint)ipBytes [0];
    }
    
    0 讨论(0)
  • 2020-12-30 14:09

    Shouldn't it be:

    var ipAddress = IPAddress.Parse("some.ip.address");
    var ipBytes = ipAddress.GetAddressBytes();
    var ip = (uint)ipBytes [0] << 24;
    ip += (uint)ipBytes [1] << 16;
    ip += (uint)ipBytes [2] <<8;
    ip += (uint)ipBytes [3];
    

    ?

    0 讨论(0)
  • 2020-12-30 14:15

    I have never found a clean solution (i.e.: a class / method in the .NET Framework) for this problem. I guess it just isn't available except the solutions / examples you provided or Aku's example. :(

    0 讨论(0)
提交回复
热议问题