Using java to convert IP addresses to decimal numbers

Source: Internet
Author: User

First look at the instance
The Code is as follows: Copy codeThe Code is as follows: classip
{
Privatestaticlongiptolong (stringstrip)
// Convert an IP address in the format of 127.0.0.1 to a 10-digit integer. No error processing 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 10-digit integer to an IP address in the format of 127.0.0.1. Enter ping3104362403l in the command prompt.
{
Stringbuffersb = newstringbuffer ("");
Sb. append (string. valueof (longip >>> 24); // shifts the value of 24 digits to the right.
Sb. append (".");
Sb. append (string. valueof (longip & 0x00ffffff) >>> 16); // move the 8-bit height to 0, and then shift the 16-bit value 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 address representation: rn ");
System. out. print ("32-bit binary format :");
System. out. println (long. tobinarystring (3366362403l ));
System. out. print ("decimal format :");
System. out. println (iptolong ("202.112.96.163 "));
System. out. print ("normal format :");
System. out. println (longtoip (3366362403l ));
}
}

Running result:
IP address representation:
32-bit binary: 11001010011100000110000010100011
Decimal format: 3396362403
Common Format: 202.112.96.163.
Output completed (1 second)-normal termination
Let's further analyze it step by step.
Knowledge Point: a binary number, shifts n places left by bit, that is, multiplying the value of this number by the Npower of 2
Shifts one digit right after division of Binary
1. convert an IP address to an integer
Principle: each segment of an IP address can be regarded as an 8-bit unsigned integer, that is, 0-255. Each segment is split into a binary array and then converted into a binary number.
An unsigned 32 is an integer.
For example, an IP address is 10.0.3.193.
The binary number corresponding to each digit.
1000001010
000000000
300000011
19311000001
After the IP address is combined, the value is 00001010000000000000001111000001, and the value is 167773121, that is, the number after the IP address is converted.
The Code is as follows:Copy codeThe Code is 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
Principle: convert this integer into a 32-bit binary number. Split each eight bits from left to right to get the 4-Segment 8-bit binary number. convert these binary numbers into integers and then add "." This is the IP address.
Example: 167773121
Binary representation: 00001010000000000000001111000001
Split into four segments:, and. convert them into integers and add "." 10.0.3.193 is displayed.
The Code is as follows:Copy codeThe Code is 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 ();
}
}

Related Article

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.