IP address conversion to decimal Implementation Program-PHP source code

Source: Internet
Author: User
The IP address in the IP address database downloaded from the Internet is not in our common format (61.175.232.30), but in this (1034938398) type of data. It seems that the two data are unrelated and complex, in fact, it is not complicated. The IP address in the IP address database downloaded from the Internet is not a common format (61.175.232.30), but a data of the (1034938398) type. It seems that the two data items are unrelated and complex, actually not complicated

Script ec (2); script

Take the IP address "61.175.232.30" as an example. to convert an IP address to a numeric string, convert "61.175.232.30" to a hexadecimal value "3d. af. "e8.1e", then remove the decimal point and change it to "3dafe81e". Finally, convert the hexadecimal number to "1034938398", and then "61.175.232.30" to "1034938398.


Method 1:

The Code is as follows:

Public function ipToLong (){
$ Ip = $ _ SERVER ['remote _ ADDR '];
$ Ip = explode ('.', $ ip );
$ Ip = array_reverse ($ ip); // array Inversion
$ R = 0;
For ($ I = 0, $ j = count ($ ip); $ I <$ j; $ I ++ ){
$ R + = $ ip [$ I] * POWs (256, $ I );
}
$ R = sprintf ("% u", $ r );
Echo $ r;
}

Method 2:

The Code is as follows:

Public function ipToLong (){
$ Ip = $ _ SERVER ['remote _ ADDR '];
$ Ip = explode ('.', $ ip );
$ R = ($ ip [0] <24) | ($ ip [1] <16) | ($ ip [2] <8) | $ ip [3];
If ($ r <0) $ r ++ = 4294967296;
Echo $ r;
}

The local ip address tested is 192.168.1.101, And the Conversion Result of the two methods is 3232235877.

Ping 192.168.1.101 is the same as ping 3232235877.


The following code uses C # To convert IP addresses:

Code

The Code is as follows:

// Ip address
String Ip = "61.175.232.30 ";
// Remove the string array after '.' from the IP address.
String [] Ip_List = Ip. Split (".". ToCharArray ());
String X_Ip = "";
// Cyclically array, convert the data to a hexadecimal number, and combine the Array (3dafe81e)
Foreach (string ip in Ip_List)
{
X_Ip + = Convert. ToInt16 (ip). ToString ("x ");
}

// Convert the hexadecimal number to the decimal number (1034938398)
Long N_Ip = long. Parse (X_Ip, System. Globalization. NumberStyles. HexNumber );

OK, the conversion is successful. Believe it or not, you can test it with the command line tool of windows. The ping 61.175.232.30 and ping 1034938398 have the same effect and all point to 61.175.232.30.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.