Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

January 2009
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

XML Feeds

Authors

« Npgsql2 the PostgreSQL Net Data provider written 100% in C# has been releasedFree SQL Server Books »
The Data Management Journal

How To Convert IP Addresses Between Bigint and Varchar

by SQLDenis


Permalink 05 Oct 2008 12:06 , Categories: Data Modelling & Design Tags: how to, sql, sql server 2008, tip

Before we start with code let us take a sample IP address, does 127.0.0.1 look familiar? Yes that is your local IP address.

Here it is in decimal and binary
127 0 0 1
01111111 00000000 00000000 00000001

Now to convert, you would take the first value,
add the second value + 256
add the third value + (256 * 256) = 65536
add the fourth value + (256 * 256 * 256) =16777216

So in our case the select would be

  1. SELECT
  2. 1 +
  3. 0 * 256 +
  4. 0 * 65536 +
  5. 127 * 16777216

which is
2130706433

So to convert from IP Adress to integer is very simple, you use PARSENAME to split it up and do the math. Here is the function.

  1. CREATE FUNCTION dbo.IPAddressToInteger (@IP AS VARCHAR(15))
  2. RETURNS BIGINT
  3. AS
  4. BEGIN
  5.  RETURN (CONVERT(BIGINT, PARSENAME(@IP,1)) +
  6.          CONVERT(BIGINT, PARSENAME(@IP,2)) * 256 +
  7.          CONVERT(BIGINT, PARSENAME(@IP,3)) * 65536 +
  8.          CONVERT(BIGINT, PARSENAME(@IP,4)) * 16777216)
  9.  
  10. END
  11. GO

But how do you get 127.0.0.1 out of 2130706433?
It is the reversed of what we did before (surprise) so instead of multiplying we will be dividing
Here is the funcion

  1. CREATE FUNCTION dbo.IntegerToIPAddress (@IP AS BIGINT)
  2. RETURNS VARCHAR(15)
  3. AS
  4. BEGIN
  5.  DECLARE @Octet1 TINYINT
  6.  DECLARE @Octet2 TINYINT
  7.  DECLARE @Octet3 TINYINT
  8.  DECLARE @Octet4 TINYINT
  9.  DECLARE @RestOfIP BIGINT
  10.  
  11.  SET @Octet1 = @IP / 16777216
  12.  SET @RestOfIP = @IP - (@Octet1 * 16777216)
  13.  SET @Octet2 = @RestOfIP / 65536
  14.  SET @RestOfIP = @RestOfIP - (@Octet2 * 65536)
  15.  SET @Octet3 = @RestOfIP / 256
  16.  SET @Octet4 = @RestOfIP - (@Octet3 * 256)
  17.  
  18.  RETURN(CONVERT(VARCHAR, @Octet1) + '.' +
  19.         CONVERT(VARCHAR, @Octet2) + '.' +
  20.         CONVERT(VARCHAR, @Octet3) + '.' +
  21.         CONVERT(VARCHAR, @Octet4))
  22. END

Now let's try this out, first run this

  1. SELECT dbo.IPAddressToInteger('127.0.0.1')

That returns 2130706433
Now run this

  1. SELECT dbo.IntegerToIPAddress(2130706433)

That returns 127.0.0.1

Thanks to K. Brian Kelley for the inspiration for this post, you can also check http://www.truthsolutions.com/ to see some of his books

And also check out the related Order IP Addresses wiki article which I wrote a while ago

2 comments »Send a trackback » 746 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

2 comments

Comment from: Emtucifor [Member] Email
You might change the backquote to a straight single quote. ‘ -> '
10/05/08 @ 14:23
Comment from: SQLDenis [Member] Email
I did change it and it looks fine in code view, bizarre ??
10/05/08 @ 16:17

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)