IP Address Translation decimal number implementation code in Java

Source: Internet
Author: User
Tags error handling split tostring stringbuffer

See instances first

  code is as follows
class IP
{
private static long Iptolong (string strip)
//Convert 127.0.0.1 IP Address to a 10-binary integer, there is no Line any error handling
{
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 a 10-in-integer form to an IP address in 127.0.0.1 form, enter ping 3396362403l at the command prompt
{
StringBuffer sb=new stringbuffer ("");
Sb.append (string.valueof (longip>>>24)), and/or directly to the right 24-bit
Sb.append (".");
Sb.append (string.valueof (longip&0x00ffffff) (>>>16)); Place a height of 8 digits to 0, then move the 16-bit
Sb.append (".") to the right;
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 ("Various forms 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

10 00001010

0 00000000

3 00000011

193 11000001

The combination is: 00001010 00000000 00000011 11000001, converted to 10 is: 167773121, that is, the IP address after the conversion of the number is it.

  code is as follows  
public class Ip {  
    public static void Main (string[] args) {&NB sp; 
        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 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

Binary representations are: 00001010 00000000 00000011 11000001

Split into four segments: 00001010,00001010,00000011,11000001, converted to integers, plus ". "I got 10.0.3.193.

The code is as follows

    public class Ip {  
    public static void Main (string[] args) {   
        System.out.print (Int2ip (167773121));  
    }  
 
   public static String Int2ip (long ipint) {  
 & nbsp;     StringBuilder sb = new StringBuilder ();  
         sb.append (Ipint & 0xFF). Append (".");   
        sb.append ((ipint >> 8) & 0xFF). Append (".");   
        sb.append ((Ipint >>) & 0xFF). Append (".");   
        sb.append ((Ipint >>) & 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.