IP resever long

Source: Internet
Author: User

Reprinted: "http://stephen830.javaeye.com/blog/254742"
When analyzing the customer source of the website, it is often required to determine the country or city location of the customer based on the customer's IP address. Of course, to do this, you need to have a detailed IP address library.

In the IP address library, the country or city is usually divided by the number (long integer) converted from the IP address. The general format of the IP address library database is:

...
Startiplongnumber endiplongnumber countryname countrycode cityname
...

In the application, the customer's IP address string must be converted into a long integer before it can be searched in the address library.

The following describes how to convert an IP address from a long to a long IP address.

Java code {
Function onclick ()
{
DP. Sh. toolbar. copytoclipboard (this); Return false;
}
} "Href =" http://www.javaeye.com/topic/254742 ">
  1. /**
  2. * The IP address is converted to an integer.
  3. * @ Param IP
  4. * @ Return
  5. */
  6. Public Static LongIp2long (string IP ){
  7. String [] IPS = IP. Split ("[.]");
  8. LongNum = 16777216l * long. parselong (IPS [0]) + 65536l * long. parselong (IPS [1]) + 256 * long. parselong (IPS [2]) + long. parselong (IPS [3]);
  9. ReturnNum;
  10. }
  11. /**
  12. * Convert an integer to an IP address.
  13. * @ Param iplong
  14. * @ Return
  15. */
  16. Public StaticString long2ip (LongIplong ){
  17. // Long iplong = 1037591503;
  18. LongMask [] = {0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 };
  19. LongNum = 0;
  20. Stringbuffer ipinfo =NewStringbuffer ();
  21. For(IntI = 0; I <4; I ++ ){
  22. Num = (iplong & Mask [I])> (I * 8 );
  23. If(I> 0) ipinfo. insert (0 ,".");
  24. Ipinfo. insert (0, long. tostring (Num, 10 ));
  25. }
  26. ReturnIpinfo. tostring ();
  27. }
/*** Convert the IP address to an integer. * @ Param IP * @ return */public static long ip2long (string IP) {string [] IPS = IP. split ("[.] "); long num = 16777216l * long. parselong (IPS [0]) + 65536l * long. parselong (IPS [1]) + 256 * long. parselong (IPS [2]) + long. parselong (IPS [3]); Return num;}/*** convert an integer to an IP address. * @ Param iplong * @ return */public static string long2ip (long iplong) {// long iplong = 1037591503; long mask [] = {0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }; long num = 0; stringbuffer ipinfo = new stringbuffer (); For (INT I = 0; I <4; I ++) {num = (iplong & Mask [I])> (I * 8); if (I> 0) ipinfo. insert (0 ,". "); ipinfo. insert (0, long. tostring (Num, 10);} return ipinfo. tostring ();}

Using the above two methods, you can easily convert an IP address string into a long number, or restore a long number into an IP address string.

 

Reprinted: http://www.javaeye.com/topic/254742

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.