JAVA "Long value and IP address mutual"-IP discrimination

Source: Internet
Author: User



Read the premise: you need to know what the IP address

Purpose of this chapter: to realize the mutual transfer of IP address and long type value


I. Scope of APPLICATION

Typically used in requirements such as login restrictions, locating IP cities, and so on, Windows's ping command also supports IP in integer form.

Second, key technical points

Here's how to convert an IP address into an integer:

1. Find the dot in the IP string using the IndexOf method of string "." The location.

2, according to the location of the point, the IP string is divided into 4 segments using the substring method of string.

3. Use Long's Parselong method to convert the segment into a 3-bit integer.

4, by the left shift operation (<<) to the number of each paragraph weighted, the first paragraph of the right to 2 of 24, the second paragraph of the right to 2 16, the third paragraph of the right 2 of the party, the last paragraph of the right 8

The method of converting an integer IP address into a string is as follows:

1, the integer value of the right shift operation (>>>), 24-bit right shift, the right shift when the high 0, the resulting number is the first IP.

2, by the operator (&) The integer value of the high 8 bits set to 0, and then the right to move 16 bits, the resulting number is the second IP.

3, through with the operator bar integer value of the high 16 bits set to 0, and then the right to move 8 bits, the resulting number is the third IP.

4, with the operator bar integer value of the high 24 bits set to 0, the resulting number is the fourth IP.



public class iptolong {           // Converts an IP address in the form of 127.0.0.1 to a decimal integer without any error handling      public static long iptolong (String strip) {         long[] ip = new long[4];          //Locate the IP address string first. Location           int position1 = strip.indexof (".");          int position2 = strip.indexof (".",  POSITION1&NBSP;+&NBSP;1);         int position3 =  Strip.indexof (".",  position2 + 1);          // Converts a 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];     }         // Converts a decimal integer to an IP address in the form of 127.0.0.1      public static string longtoip (long &NBSP;LONGIP) {         stringbuffer sb = new  StringBuffer ("");          //direct Right shift 24-bit           sb.append (StRing.valueof ((longip >>> 24)));          Sb.append (".");          //will be high 8 position 0, then move right 16 bits           sb.append (String.valueof ((LONGIP&NBSP;&AMP;&NBSP;0X00FFFFFF)  >>> 16));          sb.append (".");          //will be high 16 position 0, then move right 8 bits           sb.append (String.valueof ((LONGIP&NBSP;&AMP;&NBSP;0X0000FFFF)  >>> 8));          sb.append (".");          //will be high 24 position 0          sb.append (String.valueof ((LONGIP&NBSP;&AMP;&NBSP;0X000000FF));          return sb.tostring ();      }    &nbSp;    public static void main (String[] args) {          String ipStr =  "58.50.24.78";          long longip = iptolong.iptolong (IPSTR);          system.out.println ("integer form:"  + longip);          system.out.println ("integer"  + longIp +  "Convert to string IP address:"                   + iptolong.longtoip (longIp)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//IP address translated into binary form output           system.out.println ("binary form:"  + long.tobinarystring (LONGIP));      } }

JAVA "Long value and IP address mutual"-IP discrimination

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.