Java to achieve IP address and decimal number conversion
- 2020-04-01 01:26:19
- OfStack
See first instance
The following code
Operation results:
Various forms of IP address:
A 32-bit binary form: 11001010011100000110000010100011
Decimal form: 3396362403
Common form: 202.112.96.163.
Output complete (takes 1 second)- ends normally
Let's break it down one more time
A binary number, by the bit left move n bits, is to multiply the value of the number by 2 to the n power
Binary divide by two is to shift to the right by one bit
1, IP address conversion to integer
How it works: each segment of an IP address can be thought of as an 8-bit unsigned integer from 0 to 255
An unsigned 32 is an integer.
Example: an IP address is 10.0.3.193
The binary number corresponding to each digit
1000001010
000000000
300000011
19311000001
Combination is: 00001010000000000000001111000001, converted to decimal is: 167773121, namely the IP address the converted digital is it.
The following code
2, integer conversion to IP address
How it works: convert the integer into a 32-bit binary number. Divide every 8 bits from left to right to get 4 8-bit bits of binary Numbers, convert those bits into integers and then add ". That's the IP address
For example: 167773121
Binary representation is: 00001010000000000000001111000001
Divide into four segments: 00001010,00001010,00000011,11000001, convert to integers and add "." You get 10.0.3.193.
The following code
The following code
classip
{
privatestaticlongiptolong(stringstrip)
//Convert the 127.0.0.1 form of the IP address to a decimal integer without any error handling
{
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 base 10 integer form to an IP address in the form of 127.0.0.1 and type ping3396362403l at the command prompt
{
stringbuffersb=newstringbuffer("");
sb.append(string.valueof(longip>>>24));//Move 24 bits to the right
sb.append(".");
sb.append(string.valueof((longip&0x00ffffff)>>>16));//I'm going to go up 8 at 0, and then I'm going to go 16 to the right
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("ip Various forms of 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(" General form: ");
system.out.println(longtoip(3396362403l));
}
}
Operation results:
Various forms of IP address:
A 32-bit binary form: 11001010011100000110000010100011
Decimal form: 3396362403
Common form: 202.112.96.163.
Output complete (takes 1 second)- ends normally
Let's break it down one more time
A binary number, by the bit left move n bits, is to multiply the value of the number by 2 to the n power
Binary divide by two is to shift to the right by one bit
1, IP address conversion to integer
How it works: each segment of an IP address can be thought of as an 8-bit unsigned integer from 0 to 255
An unsigned 32 is an integer.
Example: an IP address is 10.0.3.193
The binary number corresponding to each digit
1000001010
000000000
300000011
19311000001
Combination is: 00001010000000000000001111000001, converted to decimal is: 167773121, namely the IP address the converted digital is it.
The following code
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, integer conversion to IP address
How it works: convert the integer into a 32-bit binary number. Divide every 8 bits from left to right to get 4 8-bit bits of binary Numbers, convert those bits into integers and then add ". That's the IP address
For example: 167773121
Binary representation is: 00001010000000000000001111000001
Divide into four segments: 00001010,00001010,00000011,11000001, convert to integers and add "." You get 10.0.3.193.
The following code
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();
}
}