Code for converting IP addresses to decimal numbers in java

Source: Internet
Author: User
Tags stringbuffer

First look at the instance

The code is as follows: Copy code

Class ip
{
Private static long iptolong (string strip)
// Convert an IP address in the format of 127.0.0.1 to a 10-digit integer. No error processing is performed here.
{
Int j = 0;
Int I = 0;
Long [] ip = new long [4];
Int position1 = strip. indexof (".");
Int position2 = strip. indexof (".", position1 + 1 );
Int position3 = 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
}
Private static string longtoip (long longip)
// Convert the 10-digit integer to an IP address in the format of 127.0.0.1. Enter ping 3104362403l at the command prompt.
{
Stringbuffer sb = new stringbuffer ("");
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 (".");
Return sb. tostring ();
}
Public static void main (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.

10 00001010

0 00000000

3 00000011

193 11000001

In combination, the IP address is 00001010 00000000 00000011 11000001, and the value is 167773121, that is, the number after the IP address is converted.

The code is as follows: Copy code
Public class Ip {
Public static void main (String [] args ){
System. out. print (ip2int ("10.0.3.193 "));
    }  
 
Public static long ip2int (String ip ){
String [] items = ip. split ("\.");
Return Long. 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: 00001010 00000000 00000011 11000001

Split into four segments:, and. Convert them into integers and add "." 10.0.3.193 is displayed.

The code is as follows: Copy code

Public class Ip {
Public static void main (String [] args ){
System. out. print (int2ip (167773121 ));
    }  
 
Public static String int2ip (long ipInt ){
StringBuilder sb = new StringBuilder ();
Sb. append (ipInt & 0xFF). append (".");
Sb. append (ipInt> 8) & 0xFF). append (".");
Sb. append (ipInt> 16) & 0xFF). append (".");
Sb. append (ipInt> 24) & 0xFF );
Return sb. 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.