Java implementation of IP address and decimal number of mutual conversion _java

Source: Internet
Author: User
Tags error handling
First look at the example
The code is as follows
Copy Code code as follows:

Classip
{
Privatestaticlongiptolong (Stringstrip)
Converts an IP address in the form 127.0.0.1 to a 10-binary 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]&LT;&LT;24) + (ip[1]<<16) + (ip[2]<<8) +IP[3];//IP1*256*256*256+IP2*256*256+IP3*256+IP4
}
Privatestaticstringlongtoip (LONGLONGIP)
Converts a 10-in-integer form to an IP address in 127.0.0.1 form, entering ping3396362403l at a command prompt
{
Stringbuffersb=newstringbuffer ("");
Sb.append (string.valueof (longip>>>24))/Direct 24-bit right shift
Sb.append (".");
Sb.append (string.valueof (LONGIP&AMP;0X00FFFFFF) >>>16);//will be 8 digits high 0, then 16 digits to the right
Sb.append (".");
Sb.append (string.valueof (LONGIP&AMP;0X0000FFFF) >>>8));
Sb.append (".");
Sb.append (string.valueof (LONGIP&AMP;0X000000FF));
Sb.append (".");
Returnsb.tostring ();
}
Publicstaticvoidmain (String[]args)
{
System.out.println ("Various representations 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 ("ordinary form:");
System.out.println (Longtoip (3396362403l));
}
}

Run Result:
Various representations of IP addresses:
32-bit binary form: 11001010011100000110000010100011
Decimal form: 3396362403
General form: 202.112.96.163.
Output complete (time consuming 1 seconds)-Normal termination
Let's take another step apart to analyze
Knowledge Point: A binary number, bitwise left n bit, is to multiply the value of the number by 2 of the n-th side
Binary is one of the two right shifts
1. Convert IP address to integer
Principle: IP address can be considered as a 8-bit unsigned integer that is 0-255, split each segment into a binary form, and then convert the binary number to
An unsigned 32 is an integer.
Example: An IP address for 10.0.3.193
Number of binary digits corresponding to each segment
1000001010
000000000
300000011
19311000001
The combination is: 00001010000000000000001111000001, conversion to 10 is: 167773121, that is, the IP address after the conversion of the number is it.
The code is as follows
Copy 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 integer to IP address
Principle: Converts this integer to a 32-bit binary number. From left to right, every 8 bits are split, get 4 8-bit binary numbers, convert these binary numbers to integers and add ". "That's the IP address.
Example: 167773121
The binary representation is: 00001010000000000000001111000001
Split into four segments: 00001010,00001010,00000011,11000001, converted to integers, plus ". "I got 10.0.3.193.
The code is as follows
Copy 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 ();
}
}

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.