You could also use the built in functions INET_ATON and INET_NTOA documented at 

http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

These will do the work for you

Regards

#

INET_ATON(expr)

Given the dotted-quad representation of a network address as a string, returns 
an integer that represents the numeric value of the address. Addresses may be 
4- or 8-byte addresses.

mysql> SELECT INET_ATON('209.207.224.40');
        -> 3520061480

The generated number is always in network byte order. For the example just 
shown, the number is calculated as 209×2563 + 207×2562 + 224×256 + 40.

INET_ATON() also understands short-form IP addresses:

mysql> SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');
        -> 2130706433, 2130706433

NOTE: When storing values generated by INET_ATON(), it is recommended that you 
use an INT UNSIGNED column. If you use a (signed) INT column, then values 
corresponding to IP addresses for which the first octet is greater than 127 
will be truncated to 2147483647 (that is, the value returned by 
INET_ATON('127.255.255.255')). See Section 11.2, "Numeric Types".
#

INET_NTOA(expr)

Given a numeric network address (4 or 8 byte), returns the dotted-quad 
representation of the address as a string.

mysql> SELECT INET_NTOA(3520061480);
        -> '209.207.224.40'


David Logan
Database Administrator
HP Managed Services
148 Frome Street,
Adelaide 5000
Australia

+61 8 8408 4273 - Work
+61 417 268 665 - Mobile
+61 8 8408 4259 - Fax


-----Original Message-----
From: Evan Borgstrom [mailto:[EMAIL PROTECTED] 
Sent: Saturday, 12 November 2005 7:13 AM
To: Cory @ SkyVantage
Cc: [EMAIL PROTECTED]; MySql; [EMAIL PROTECTED]
Subject: Re: Best Fieldtype to store IP address...


        The same way the kernel deals with them; int(10) unsigned. To convert a
dotted quad string into int(10) use the following:

Using 192.168.10.50:
192 + (168 * 2^8) + (10 * 2^16) + (50 * 2^24) = 839559360

        This is real handy if you're doing low level socket stuff and storing
addresses in the database.

-Evan

Cory @ SkyVantage wrote:
> I'm using MySQL-Cluster 5.0, and we're doing some research.
> What is everyone's opinion as to what the best fieldtype to store an IP
> address in?
> 
> varchar(16)  ?  because 16 is the max chars of an ip address...
> char(16) ?
> text(16)
> 
> Not quite sure how to get the best memory utilization...
> 
> 

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to