Convert an IP address to an integer
// Accept a parameter of the string type and convert it to a parameter of the long type.
// Temporarily convert string-type IP addresses
Public long iptolong (string IP ){
String [] IPS = IP. Split ("//.");
Stringbuffer strip = new stringbuffer ();
For (INT I = 0; I <IPs. length; I ++ ){
Strip. append (IPS [I]);
}
Return long. parselong (strip. tostring ());
}
When the above conversions occur for 192.168.12.1 and 192.168.1.21, the results are the same: 192168121
The following two methods are available:
First:
The ID is divided into four sections. If each segment is not 3 characters long, add zero, for example, 192.168.12.1 and 192.168.1.21. The result is 192168012001 and 192168001021.
Code:
Package com. Text. Dos;
Import org. Omg. portableinterceptor. Discarding;
Public class test {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo automatically generates method stubs
String S = "192.168.1.12 ";
Long J = dostring (s );
System. Out. println (j );
}
Private Static long dostring (string idstring)
{
Long I = 0;
Int a1 = idstring. indexof (".");
Int a2 = idstring. indexof (".", A1 + 1 );
Int a3 = idstring. indexof (".", A2 + 1 );
String S1 = idstring. substring (0, A1 );
String S2 = idstring. substring (A1 + 1, A2 );
String S3 = idstring. substring (A2 + 1, A3 );
String S4 = idstring. substring (A3 + 1, idstring. Length ());
// System. Out. println (S4 );
If (s1.length () = 1)
S1 = "00" + S1;
Else if (s1.length () = 2 ){
S1 = "0" + S1;
}
System. Out. println (S1 );
If (s2.length () = 1)
S2 = "00" + S2;
Else if (s2.length () = 2 ){
S2 = "0" + S2;
}
System. Out. println (S2 );
If (s3.length () = 1)
S3 = "00" + S3;
Else if (s3.length () = 2 ){
S3 = "0" + S3;
}
If (s4.length () = 1)
S4 = "00" + S4;
Else if (s4.length () = 2 ){
S4 = "0" + S4;
}
System. Out. println (S3 );
String S = S1 + S2 + S3 + S4;
I = long. parselong (s );
Return I;
}
}
Second:
I. Application Scope
Generally, this command is used for Logon Restrictions and IP address city search. The ping command for Windows also supports an integer IP address.
II. Key Technical Points
The method to convert an IP address to an integer is as follows:
1. Use the indexof method of string to locate the "." position in the IP string.
2. Use the string substring method to divide the IP string into four segments based on the position of the vertex.
3. Use the parselong method of long to convert the child segment into a three-digit integer.
4. The left shift operation (<) is used to weight the number of each segment. The first segment has the 24 power of 2 and the second segment has the 16 power of 2, the right in the third section is the 8 power of 2, and the right in the last section is 1.
The following describes how to convert an integer-type IP address to a string:
1. Perform the right shift operation on the integer (>>>), shift the value to the right 24 bits, and add 0 to the height when shift to the right. The obtained number is the first IP address.
2. Set the 8-bit high of the integer to 0 with the operator (&), and move the 16-bit value to the right. The obtained number is the second IP address.
3. Set the 16-bit high of the integer with the operator to 0, and then shift the 8-bit right to the third IP address.
4. Set the 24-bit high of the integer with the operator to 0, and the resulting number is the fourth IP address.
Iii. instance demonstration
Public class ip2long ...{
// Convert an IP address in the format of 127.0.0.1 to a decimal integer. No error processing is performed here.
Public static long iptolong (string strip )...{
Long [] IP = new long [4];
// First locate the. Position in the IP address string
Int position1 = strip. indexof (".");
Int position2 = strip. indexof (".", position1 + 1 );
Int position3 = strip. indexof (".", position2 + 1 );
// Convert the string between each. To an integer.
IP [0] = long. parselong (strip. substring (0, position1 ));
IP [1] = long. parselong (strip. substring (position1 + 1, position2 ));
IP [2] = long. parselong (strip. substring (position2 + 1, position3 ));
IP [3] = long. parselong (strip. substring (position3 + 1 ));
Return (IP [0] <24) + (IP [1] <16) + (IP [2] <8) + IP [3];
}
// Converts an IP address in the format of 127.0.0.1 to a decimal integer.
Public static string longtoip (long longip )...{
Stringbuffer sb = new stringbuffer ("");
// Shifts the value of 24 digits to the right.
SB. append (string. valueof (longip >>> 24 )));
SB. append (".");
// Move the height of 8 to 0, and then shift the right to 16
SB. append (string. valueof (longip & 0x00ffffff) >>> 16 ));
SB. append (".");
// Change the height of 16 to 0, and then shift the right to 8 places
SB. append (string. valueof (longip & 0x0000ffff) >>> 8 ));
SB. append (".");
// Set the height to 24 and 0
SB. append (string. valueof (longip & 0x000000ff )));
Return sb. tostring ();
}
/***//**
* @ Param ARGs
*/
Public static void main (string [] ARGs )...{
String ipstr = "192.168.0.1 ";
Long longip = ip2long. iptolong (ipstr );
System. Out. println ("the integer form of 192.168.0.1 is:" + longip );
System. Out. println ("integer" + longip + "convert to string IP Address :"
+ Ip2long. longtoip (longip ));
// Convert the IP address to binary format for output
System. Out. println ("the binary format of 192.168.0.1 is:" + long. tobinarystring (longip ));
}
}