Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I normally just use varchar(15) for IPv4 addresses - but sorting them is a pain unless you pad zeros.</p> <p>I've also stored them as an INT in the past. <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx" rel="noreferrer"><code>System.Net.IPAddress</code></a> has a <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.getaddressbytes.aspx" rel="noreferrer"><code>GetAddressBytes</code></a> method that will return the IP address as an array of the 4 bytes that represent the IP address. You can use the following C# code to convert an <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx" rel="noreferrer"><code>IPAddress</code></a> to an <code>int</code>... </p> <pre><code>var ipAsInt = BitConverter.ToInt32(ip.GetAddressBytes(), 0); </code></pre> <p>I had used that because I had to do a lot of searching for dupe addresses, and wanted the indexes to be as small &amp; quick as possible. Then to pull the address back out of the int and into an <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx" rel="noreferrer"><code>IPAddress</code></a> object in .net, use the <a href="http://msdn.microsoft.com/en-us/library/system.bitconverter.getbytes.aspx" rel="noreferrer"><code>GetBytes</code></a> method on <a href="http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx" rel="noreferrer"><code>BitConverter</code></a> to get the int as a byte array. Pass that byte array to the <a href="http://msdn.microsoft.com/en-us/library/t4k07yby.aspx" rel="noreferrer">constructor</a> for <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx" rel="noreferrer"><code>IPAddress</code></a> that takes a byte array, and you end back up with the <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx" rel="noreferrer"><code>IPAddress</code></a> that you started with.</p> <pre><code>var myIp = new IPAddress(BitConverter.GetBytes(ipAsInt)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload