<p>I'm writing C# code that uses the windows IP Helper API. One of the functions I'm trying to call is "<a href="http://msdn.microsoft.com/en-us/library/aa365920(VS.85).aspx" rel="nofollow noreferrer">GetBestInterface</a>" that takes a 'uint' representation of an IP. What I need is to parse a textual representation of the IP to create the 'uint' representation.</p>

<p>I've found some examples via Google, like <a href="http://www.justin-cook.com/wp/2006/11/28/convert-an-ip-address-to-ip-number-with-php-asp-c-and-vbnet/" rel="nofollow noreferrer">this one</a> or <a href="http://www.codeguru.com/csharp/.net/net_general/internet/article.php/c10651" rel="nofollow noreferrer">this one</a>, but I'm pretty sure there should be a standard way to achieve this with .NET. Only problem is, I can't find this standard way. IPAddress.Parse seems to be in the right direction, but it doesn't supply any way of getting a 'uint' representation...</p>

<p>There is also a way of doing this using IP Helper, using the <a href="http://msdn.microsoft.com/en-us/library/bb408412(VS.85).aspx" rel="nofollow noreferrer">ParseNetworkString</a>, but again, I'd rather use .NET - I believe the less I rely on pInvoke the better.</p>

<p>So, anyone knows of a standard way to do this in .NET?</p>

[{'answer_id': 36841, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 5, 'selected': True, 'text': '<p>MSDN <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.getaddressbytes.aspx" rel="noreferrer">says</a> that IPAddress.Address property (which returns numeric representation of IP address) is obsolete and you should use <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.getaddressbytes.aspx" rel="noreferrer">GetAddressBytes</a> method.</p>\n\n<p>You can convert IP address to numeric value using following code:</p>\n\n<pre><code>var ipAddress = IPAddress.Parse("some.ip.address");\nvar ipBytes = ipAddress.GetAddressBytes();\nvar ip = (uint)ipBytes [3] &lt;&lt; 24;\nip += (uint)ipBytes [2] &lt;&lt; 16;\nip += (uint)ipBytes [1] &lt;&lt;8;\nip += (uint)ipBytes [0];\n</code></pre>\n\n<p><strong>EDIT:</strong><br>\nAs other commenters noticed above-mentioned code is for IPv4 addresses only.\nIPv6 address is 128 bits long so it\'s impossible to convert it to \'uint\' as question\'s author wanted.</p>\n'}, {'answer_id': 36843, 'author': 'Andrei Rînea', 'author_id': 1796, 'author_profile': 'https://Stackoverflow.com/users/1796', 'pm_score': 0, 'selected': False, 'text': "<p>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. :(</p>\n"}, {'answer_id': 36845, 'author': 'Dmitry Shechtman', 'author_id': 3583, 'author_profile': 'https://Stackoverflow.com/users/3583', 'pm_score': 1, 'selected': False, 'text': '<p>Byte arithmetic is discouraged, as it relies on all IPs being 4-octet ones.</p>\n'}, {'answer_id': 36871, 'author': 'Corin Blaikie', 'author_id': 1736, 'author_profile': 'https://Stackoverflow.com/users/1736', 'pm_score': 3, 'selected': False, 'text': '<p>Also you should remember that <a href="http://en.wikipedia.org/wiki/IPv4" rel="noreferrer">IPv4</a> and <a href="http://en.wikipedia.org/wiki/IPv6" rel="noreferrer">IPv6</a> are different lengths.</p>\n'}, {'answer_id': 37231, 'author': 'nimish', 'author_id': 3926, 'author_profile': 'https://Stackoverflow.com/users/3926', 'pm_score': 4, 'selected': False, 'text': '<pre><code>var ipuint32 = BitConverter.ToUInt32(IPAddress.Parse("some.ip.address.ipv4").GetAddressBytes(), 0);`\n</code></pre>\n\n<p>This solution is easier to read than manual bit shifting.</p>\n\n<p>See <a href="https://stackoverflow.com/questions/461742/how-to-convert-an-ipv4-address-into-a-integer-in-c">How to convert an IPv4 address into a integer in C#?</a></p>\n'}, {'answer_id': 553315, 'author': 'Christo', 'author_id': 66948, 'author_profile': 'https://Stackoverflow.com/users/66948', 'pm_score': 4, 'selected': False, 'text': '<p>Shouldn\'t it be:</p>\n\n<pre><code>var ipAddress = IPAddress.Parse("some.ip.address");\nvar ipBytes = ipAddress.GetAddressBytes();\nvar ip = (uint)ipBytes [0] &lt;&lt; 24;\nip += (uint)ipBytes [1] &lt;&lt; 16;\nip += (uint)ipBytes [2] &lt;&lt;8;\nip += (uint)ipBytes [3];\n</code></pre>\n\n<p>?</p>\n'}, {'answer_id': 3900026, 'author': 'qasali', 'author_id': 517162, 'author_profile': 'https://Stackoverflow.com/users/517162', 'pm_score': 1, 'selected': False, 'text': '<pre><code>System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("192.168.1.1");\n\nbyte[] bytes = ipAddress.GetAddressBytes();\nfor (int i = 0; i &lt; bytes.Length ; i++)\n       Console.WriteLine(bytes[i]);\n</code></pre>\n\n<p>Output will be \n192\n168\n1\n1</p>\n'}, {'answer_id': 50188623, 'author': 'Pavel Samoylenko', 'author_id': 2399045, 'author_profile': 'https://Stackoverflow.com/users/2399045', 'pm_score': 1, 'selected': False, 'text': '<p>Complete solution:</p>\n\n<pre><code>public static uint IpStringToUint(string ipString)\n{\n    var ipAddress = IPAddress.Parse(ipString);\n    var ipBytes = ipAddress.GetAddressBytes();\n    var ip = (uint)ipBytes [0] &lt;&lt; 24;\n    ip += (uint)ipBytes [1] &lt;&lt; 16;\n    ip += (uint)ipBytes [2] &lt;&lt;8;\n    ip += (uint)ipBytes [3];\n    return ip;\n}\n\npublic static string IpUintToString(uint ipUint)\n{\n    var ipBytes = BitConverter.GetBytes(ipUint);\n    var ipBytesRevert = new byte[4];\n    ipBytesRevert[0] = ipBytes[3];\n    ipBytesRevert[1] = ipBytes[2];\n    ipBytesRevert[2] = ipBytes[1];\n    ipBytesRevert[3] = ipBytes[0];\n    return new IPAddress(ipBytesRevert).ToString();\n}\n</code></pre>\n\n<p>Reverse order of bytes:</p>\n\n<pre><code>public static uint IpStringToUint(string ipString)\n{\n    return BitConverter.ToUInt32(IPAddress.Parse(ipString).GetAddressBytes(), 0);\n}\n\npublic static string IpUintToString(uint ipUint)\n{\n    return new IPAddress(BitConverter.GetBytes(ipUint)).ToString();\n}\n</code></pre>\n\n<p>You can test here:</p>\n\n<p><a href="https://www.browserling.com/tools/dec-to-ip" rel="nofollow noreferrer">https://www.browserling.com/tools/dec-to-ip</a></p>\n\n<p><a href="http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx" rel="nofollow noreferrer">http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx</a></p>\n\n<p><a href="http://www.silisoftware.com/tools/ipconverter.php" rel="nofollow noreferrer">http://www.silisoftware.com/tools/ipconverter.php</a></p>\n'}, {'answer_id': 57980588, 'author': 'Nick', 'author_id': 984516, 'author_profile': 'https://Stackoverflow.com/users/984516', 'pm_score': 2, 'selected': False, 'text': '<p>Correct solution that observes Endianness:</p>\n\n<pre><code>var ipBytes = ip.GetAddressBytes();\nulong ip = 0;\nif (BitConverter.IsLittleEndian)\n{\n    ip = (uint) ipBytes[0] &lt;&lt; 24;\n    ip += (uint) ipBytes[1] &lt;&lt; 16;\n    ip += (uint) ipBytes[2] &lt;&lt; 8;\n    ip += (uint) ipBytes[3];\n}\nelse\n{\n    ip = (uint)ipBytes [3] &lt;&lt; 24;\n    ip += (uint)ipBytes [2] &lt;&lt; 16;\n    ip += (uint)ipBytes [1] &lt;&lt;8;\n    ip += (uint)ipBytes [0];\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/36831', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1596/']