Java programming example code for converting IP addresses and numbers

Source: Internet
Author: User

Java programming example code for converting IP addresses and numbers

I recently learned that you can access the website by converting the IP address to decimal, octal, or hexadecimal.

Convert an IP address to a number (second algorithm. Use shift left, by bit, or. Higher efficiency .) :

public long ipToLong(String ipAddress) {  long result = 0;  String[] ipAddressInArray = ipAddress.split("\\.");  for (int i = 3; i >= 0; i--) {    long ip = Long.parseLong(ipAddressInArray[3 - i]);    //left shifting 24,16,8,0 and bitwise OR    //1. 192 << 24   //1. 168 << 16   //1. 1  << 8   //1. 2  << 0   result |= ip << (i * 8);  } return result;  } 

Convert numbers to IP addresses. Both algorithms are similar:

//ip = 3232235778  public String longToIp(long ip) { StringBuilder result = new StringBuilder(15);  for (int i = 0; i < 4; i++) {    result.insert(0,Long.toString(ip & 0xff));    if (i < 3) {     result.insert(0,'.');   }    ip = ip >> 8; } return result.toString();  }   //ip = 3232235778  public String longToIp2(long ip) {  return ((ip >> 24) & 0xFF) + "."    + ((ip >> 16) & 0xFF) + "."    + ((ip >> 8) & 0xFF) + "."    + (ip & 0xFF);  } 

Complete code:

public class JavaBitwiseExample {     public static void main(String[] args) {       JavaBitwiseExample obj = new JavaBitwiseExample();       System.out.println("iptoLong : " + obj.ipToLong("192.168.1.2"));     System.out.println("iptoLong2 : " + obj.ipToLong2("192.168.1.2"));       System.out.println("longToIp : " + obj.longToIp(3232235778L));     System.out.println("longToIp2 : " + obj.longToIp2(3232235778L));     }     // example : 192.168.1.2   public long ipToLong(String ipAddress) {       // ipAddressInArray[0] = 192     String[] ipAddressInArray = ipAddress.split("\\.");       long result = 0;     for (int i = 0; i < ipAddressInArray.length; i++) {         int power = 3 - i;       int ip = Integer.parseInt(ipAddressInArray[i]);         // 1. 192 * 256^3       // 2. 168 * 256^2       // 3. 1 * 256^1       // 4. 2 * 256^0       result += ip * Math.pow(256, power);       }       return result;     }     public long ipToLong2(String ipAddress) {       long result = 0;       String[] ipAddressInArray = ipAddress.split("\\.");       for (int i = 3; i >= 0; i--) {         long ip = Long.parseLong(ipAddressInArray[3 - i]);         // left shifting 24,16,8,0 and bitwise OR         // 1. 192 << 24       // 1. 168 << 16       // 1. 1 << 8       // 1. 2 << 0       result |= ip << (i * 8);       }       return result;   }     public String longToIp(long i) {       return ((i >> 24) & 0xFF) +            "." + ((i >> 16) & 0xFF) +            "." + ((i >> 8) & 0xFF) +            "." + (i & 0xFF);     }     public String longToIp2(long ip) {     StringBuilder sb = new StringBuilder(15);       for (int i = 0; i < 4; i++) {         // 1. 2       // 2. 1       // 3. 168       // 4. 192       sb.insert(0, Long.toString(ip & 0xff));         if (i < 3) {         sb.insert(0, '.');       }         // 1. 192.168.1.2       // 2. 192.168.1       // 3. 192.168       // 4. 192       ip = ip >> 8;       }       return sb.toString();   } } 

Summary

The above is all about the Java programming IP address and the code example for converting numbers to each other. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message!

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.