Java IP address conversion

Source: Internet
Author: User
  1. /**
     
  2. * IP hexadecimal conversion tool
     
  3. *
     
  4. * @ Author ym
     
  5. */

  6. Public
     
    Class
    Ipconvert {
  7. /**
     
  8. * Set
    Convert the IP address in decimal format (for example, 192.168.1.1) to decimal format (for example, 3232235777)
     
  9. *
     
  10. * @ Param IP
     
  11. * @ Return
     
  12. */

  13. Public
     
    Static
     
    Long
    Getip10 (string IP ){
  14. Long
    Ip10 =
    0
    ;
  15. String [] Ss = IP. Trim (). Split ("//."
    );
  16. For
    (
    Int
    I =
    0
    ; I <
    4
    ; I ++ ){
  17. Ip10 + = math. Pow (256
    ,
    3
    -I) * integer. parseint (ss [I]);
  18. }
  19. Return
    Ip10;
  20. }
  21. /**
     
  22. * Set
    Convert an IP address to an IP address in decimal format (for example, 192.168.1.1)
     
  23. *
     
  24. * @ Param ip10
     
  25. * @ Return
     
  26. */

  27. Public
     
    Static
    String getip (
    Long
    Ip10 ){
  28. String IP = ""
    ;
  29. Long
    Temp =
    0
    ;
  30. For
    (
    Int
    I =
    3
    ; I> =
    0
    ; I --){
  31. Temp = ip10/(long
    ) Math. Pow (
    256
    , I) %
    256
    ;
  32. If
    (I =
    3
    ){
  33. IP = IP + temp;
  34. } Else
    {
  35. IP = IP + "."
    + Temp;
  36. }
  37. }
  38. Return
    IP address;
  39. }
  40. Public
     
    Static
     
    Void
    Main (string [] ARGs ){
  41. System. Out. println (getip10 ("192.168.1.1"
    ));
  42. System. Out. println (getip (getip10 ("192.168.1.1"
    )));
  43. }
  44. }
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.