IP Addresses to number (Integer) and reverse

Also see http://dev.mysql.com/doc/mysql/en/Miscellaneous_functions.html

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.
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*256^3 + 207*256^2 + 224*256 + 40. As of MySQL 4.1.2, INET_ATON() also understands short-form IP addresses:

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

INET_ATON() was added in MySQL 3.23.15.
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’

INET_NTOA() was added in MySQL 3.23.15.

Comments are closed.