<

Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Store IP Address in a Database

Published on
9,365 Points
3,365 Views
Last Modified:
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.  

So, let's look at a few ways we can store an IP address.

1.  Together as a string in a CHAR(15) column; e.g:
     '192.168.0.1'

2.  Four separate TINYINT columns; e.g.:
     192    168    0    1

3.  Store it as a numeric value (a single INT or LONG column); e.g:
     3232235521

The first method’s advantages are simple programming and it is directly readable, but it requires more storage space.   The second and third methods use comparatively less storage space, but they require additional programming and data handling.

Herewith I am going to share a code snip that used to store IP address as a numeric value

Convert IP address to a Numeric Value
//A.B.C.D <=> A*256^3 + B*256^2 + C*256 + D 
//        <=> A* 16777216 + B*65536 + C*256 + D 
 
long val = (long.Parse(splitArray[0]) * 16777216) + 
                 (long.Parse(splitArray[1]) * 65536) + 
                       (long.Parse(splitArray[2]) * 256) + 
                             (long.Parse(splitArray[3])); 
 
MessageBox.Show(val.ToString());

Open in new window


Convert Numeric Value to IP address
long ipRem1, ipRem2, ipRem3; 
 
// Convert the text value as long 
long convertValue = long.Parse(longValueTextBox.Text.Trim()); 
 
// Re-Calculation 
long ipAdd1 = Math.DivRem(convertValue, 16777216, out ipRem1); 
long ipAdd2 = Math.DivRem(ipRem1, 65536, out ipRem2); 
long ipAdd3 = Math.DivRem(ipRem2, 256, out ipRem3); 
 
// Construct IP address 
string ipAddress = ipAdd1.ToString().Trim() + "."  
                     + ipAdd2.ToString().Trim() + "."  
                         + ipAdd3.ToString().Trim() + "."  
                           + ipRem3.ToString().Trim(); 
 
MessageBox.Show(convertValue + " ---> " + ipAddress); 

Open in new window


Also you can check the validity of the single-value IP address by using this function:
/// <summary> 
/// Checks the validity. 
/// </summary> 
/// <param name="values">The values.</param> 
/// <returns></returns> 
private static bool CheckValidity(IEnumerable<string> values) 
{ 
    foreach(var val in values) 
    { 
        int tempval; 
 
        try 
        { 
            tempval = Int32.Parse(val); 
        } 
        catch(Exception) 
        { 
            return false; 
        } 
 
        if (tempval < 0 || tempval > 255) 
        { 
            return false; 
        } 
    } 
    return true; 
}

Open in new window

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Join & Write a Comment

In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month