// Convert IP addresses of the form 127.0.0.1 to decimal integers, no error handling is done here public static long ipToLong(String strIp) { long[] ip = new long[4]; // First find the location of . 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 decimal integer form to an ip address in the form of 127.0.0.1 public static String longToIP(long longIp) { StringBuffer sb = new StringBuffer(""); // Directly shift right by 24 bits sb.append(String.valueOf((longIp >>> 24))); sb.append("."); // Set high 8 bits to 0, then shift right by 16 bits sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16)); sb.append("."); // Set high 16 bits to 0, then shift right by 8 bits sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8)); sb.append("."); // set the high 24 position to 0 sb.append(String.valueOf((longIp & 0x000000FF))); return sb.toString(); }
ip address decimal conversion
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326682906&siteId=291194637
Recommended
Ranking