Let’s look at the example code first:
Copy the code code as follows:
classip
{
privatestaticlongiptolong(stringstrip)
//Convert the IP address in the form of 127.0.0.1 into a decimal integer. No error handling is performed here.
{
intj=0;
inti=0;
long[]ip=newlong[4];
intposition1=strip.indexof(".");
intposition2=strip.indexof(".",position1+1);
intposition3=strip.indexof(".",position2+1);
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];//ip1*256*256*256+ip2*256* 256+ip3*256+ip4
}
privatestaticstringlongtoip(longlongip)
//Convert the decimal integer form into an IP address in the form of 127.0.0.1, enter ping3396362403l at the command prompt
{
stringbuffersb=newstringbuffer("");
sb.append(string.valueof(longip>>>24));//Shift right by 24 bits directly
sb.append(".");
sb.append(string.valueof((longip&0x00ffffff)>>>>16));//Set the high 8 bits to 0, and then shift right by 16 bits
sb.append(".");
sb.append(string.valueof((longip&0x0000ffff)>>>>8));
sb.append(".");
sb.append(string.valueof(longip&0x000000ff));
sb.append(".");
returnsb.tostring();
}
publicstaticvoidmain(string[]args)
{
system.out.println("Various expressions of ip address: rn");
system.out.print("32-bit binary form: ");
system.out.println(long.tobinarystring(3396362403l));
system.out.print("Decimal form:");
system.out.println(iptolong("202.112.96.163"));
system.out.print("Common form:");
system.out.println(longtoip(3396362403l));
}
}
Running results:
Various manifestations of IP addresses:
32-bit binary form: 11001010011100000110000010100011
Decimal form: 3396362403
Common form: 202.112.96.163.
Output completed (taking 1 second) - normal termination. Let’s analyze the knowledge points step by step: a binary number, shifted left by n bits, is to multiply the value of the number by 2 raised to the nth power and divide by two, that is Shift right one position
1. Convert IP address to integer <BR>Principle: Each segment of IP address can be regarded as an 8-bit unsigned integer, that is, 0-255. Split each segment into a binary form and combine it, and then convert this binary number into a Unsigned 32 is an integer.
Example: An IP address is 10.0.3.193
The binary number corresponding to each segment of numbers
1000001010
000000000
300000011
19311000001
The combination is: 00001010000000000000001111000001. When converted to decimal, it is: 167773121, that is, the converted number of the IP address is it.
The code is as follows
Copy the code code as follows:
publicclassIp{
publicstaticvoidmain(String[]args){
System.out.print(ip2int("10.0.3.193"));
}
publicstaticlongip2int(Stringip){
String[]items=ip.split(".");
returnLong.valueOf(items[0])<<24
|Long.valueOf(items[1])<<16
|Long.valueOf(items[2])<<8
|Long.valueOf(items[3]);
}
}
2. Convert an integer to an IP address <BR>Principle: Convert this integer into a 32-bit binary number. From left to right, divide every 8 bits to get 4 segments of 8-bit binary numbers. Convert these binary numbers into integers and add "." This is the IP address. Example: 167773121
The binary representation is: 00001010000000000000001111000001
Divide it into four segments: 00001010, 00001010, 00000011, 11000001. After converting them to integers and adding ".", you get 10.0.3.193.
The code is as follows
Copy the code code as follows:
publicclassIp{
publicstaticvoidmain(String[]args){
System.out.print(int2ip(167773121));
}
publicstaticStringint2ip(longipInt){
StringBuildersb=newStringBuilder();
sb.append(ipInt&0xFF).append(".");
sb.append((ipInt>>8)&0xFF).append(".");
sb.append((ipInt>>16)&0xFF).append(".");
sb.append((ipInt>>24)&0xFF);
returnsb.toString();
}
}