//accepts a parameter of type String and converts it to a parameter of type long.
//Temporarily convert the IP address of string type.
Public length ipToLong (string IP) (
String[] ips = ip.split("//");
string buffer strip = new string buffer();
for(int I = 0; I & ltips.lengthi++){
strip . append(IPS[I]);
}
Returns long.parselong (strip.tostring ());
}
In the case of 192. 168. 1 2.192.168.1,if the above transformation is used, the result will be the same:
So there are two ways:
The first kind:
Id is divided into four parts. If each paragraph is less than 3 digits, add zero, such as: 192. 1 68.12.1and192.1,and the result is: 65438.
Code:
Package com.text.dos;
import org . OMG . portable interceptor . discarding;
Common class test {
/**
* @param args
*/
Public static void main(String[] args) {
// TODO automatically generates method stubs
string s = " 192. 168. 1. 12 ";
long j = do string(s);
system . out . println(j);
}
Private static long doString(String idString)
{
long I = 0;
int a 1 = idString.indexOf(" . ");
int a2 = idString.indexOf(" . ",a 1+ 1);
int a3 = idString.indexOf(" . ",a2+ 1);
string s 1 = id string . substring(0,a 1);
string S2 = id string . substring(a 1+ 1,a2);
string S3 = id string . substring(a2+ 1,a3);
string S4 = id string . substring(a3+ 1,id string . length());
//system . out . println(S4);
if(s 1.length()== 1)
s 1 = " 00 "+s 1;
else if (s 1.length()==2) {
s 1 = " 0 "+s 1;
}
system . out . println(s 1);
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 . parse long(s);
Return I;
}
}
The second type:
I. Scope of application
Generally used for login restrictions, finding the city where IP is located, etc. Windows ping command also supports IP in integer form.
Second, the technical points
The method of converting an IP address into an integer is as follows:
1. Find the location of the point. A string obtained by the indexOf method in an IP string.
2. According to the position of the point, the IP String is divided into four segments by using the substring method of String.
3. Use the parseLong method of Long to convert the subsection into a 3-bit integer.
4. By moving left (
The method of converting an IP address in integer form into a string is as follows:
1. Move the integer value to the right (>; & gt& gt), shift 24 bits to the right, and add 0 to the high order when shifting to the right, and the number obtained is the first IP.
2. Put the operator through (&; ) Set the upper 8 bits of the integer value to 0, and then shift 16 bits to the right to get the second IP.
3. Set the high 16 bit of the integer value of the AND operator to 0, and then shift it to the right by 8 bits to get the third IP.
4. Set the upper 24 bits of the integer value to 0 with the AND operator, and the number obtained is the fourth IP.
Third, the example demonstration
Public class IP2Long...{
//Convert the IP address in the form of 127.0.0. 1 into a decimal integer without any error handling.
Public static long ipToLong (string band) ...
long[]IP = new long[4];
//The location found. Enter the IP address string first.
int position 1 = strip . index of(" . ");
Int position2 = strIp.indexOf (".",position1+1);
Int position3 = strIp.indexOf (".",position 2+1);
//Convert the string between them. Convert to an integer.
IP[0]= long . parse long(strip . substring(0,position 1));
IP[ 1]= long . parse long(strip . substring(position 1+ 1,position 2));
IP[2]= long . parse long(strip . substring(position 2+ 1,position 3));
IP[3]= long . parse long(strip . substring(position 3+ 1));
return(IP[0]& lt; & lt24)+(IP[ 1]& lt; & lt 16)+(IP[2]& lt; & lt8)+IP[3];
}
//Convert the decimal integer form into an ip address in the form of 127.0.0. 1
Public static string longtoip (longlonggip) ...
string buffer sb = new string buffer(" ");
//Move 24 bits directly to the right.
sb . append(string . value of((longIp & gt; & gt& gt24)));
Someone adds ("." );
//Set the upper 8 bits to 0, and then shift 16 bits to the right.
The value of (longip & amp0x00ffffff) >; & gt& gt 16));
Someone adds ("." );
//Set the high 16 bit to 0, and then shift it to the right by 8 bits.
The value of (longIp & amp0x0000FFFF)>& gt& gt8>8));
Someone adds ("." );
//Set the upper 24 bits to 0.
The value of (longip & amp0x000000ff));
return sb . tostring();
}
/** *//**
* @param args
*/
Public static void main (string [] args) ...
string ipStr = " 192. 168.0 . 1 ";
long longIp = IP 2 long . iptolong(ipStr);
System. Out.println ("192.168.0.1yes"+longip in integer form);
System. out.println ("integer"+longip+"converted to string Ip address:"
+IP 2 long . long toip(longIp));
//ip address is converted into binary form for output.
System. Out.println (binary form of "192.168.0.1yes"+long.tobinarystring (longip));
}
}