Converts an IP address to an integer.

Source: Internet
Author: User
Tags string indexof

Key technologies:

The key technical points for converting an IP address to an integer are as follows:

1. Locate the "." position in the IP string using the string indexof Method
2. Use the string substring method to divide the IP string into four segments based on the position of the vertex.
3. Use the long parselong method to convert the sub-segment into a three-digit number.
4. The left shift operation (<) is used to weight the number of each segment. The first segment has the 24 power of 2 and the second segment has the 16 power of 2, the right in the third section is the 8 power of 2, and the right in the last section is 1.

The technical point of converting an integer-type IP address into a string is as follows:

1. Perform the right shift operation on the integer (>), shift the value to the right 24 bits, and add 0 to the height when shift to the right. The obtained number is the first IP address.
2. Set the high 8 bits of the integer to 0 with the operator (&), and move the 16 bits to the right. The obtained number is the second IP address.
3. Set the 16-bit high of the integer to 0 by using the operator, and then shift the 8-bit right to the third IP address.
4. Set the 24-bit high of the integer to 0 with the operator, and the resulting number is the fourth segment of the IP address.
Public class ip2long {
// Convert an IP address in the format of 127.0.0.1 to a 10-digit integer. No error processing is performed here.
Public static long iptolong (string strip ){
Long [] IP = new long [4];
// First locate the. Position in the IP address string
Int position1 = strip. indexof (".");
Int position2 = strip. indexof (".", position1 + 1 );
Int position3 = strip. indexof (".", position2 + 1 );
// Convert the string between each. To an integer.
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];
}

// Convert the 10-digit integer to an IP address in the format of 127.0.0.1
Public static String longToIP (long longIP ){
StringBuffer sb = new StringBuffer ("");
// Shifts the value of 24 digits to the right.
SB. append (string. valueof (longip >>> 24 ));
SB. append (".");
// Move the height of 8 to 0, and then shift the right to 16
SB. append (string. valueof (longip & 0x00ffffff) >>> 16 ));
SB. append (".");
SB. append (string. valueof (longip & 0x0000ffff) >>> 8 ));
SB. append (".");
SB. append (string. valueof (longip & 0x000000ff ));
Return sb. tostring ();
}
/***//**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
String ipstr = "192.168.0.1 ";
Long iplong = ip2long. iptolong (ipstr );
System. Out. println ("the integer form of 192.168.0.1 is:" + iplong );
System. Out. println ("integer" + iplong + "convert to string IP Address :"
+ Ip2long. longtoip (iplong ));
// Convert the IP address to binary format for output
System. Out. println ("the binary format of 192.168.0.1 is :"
+ Long. tobinarystring (iplong ));
}
}

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.