Java字符ip地址和数字ip地址之间的相互转换
package com.hxhk.cc.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public class IPAddressFormatter { private static final long serialVersionUID = 1364280016493483133L; private final static int Num = 32; /** * 判断点分十进制IP地址是否正确 * @param str * @return */ public static boolean isIP(String str){ Pattern p= Pattern.compile("^([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$"); Matcher m =p.matcher(str); return m.matches(); } /** * 将127.0.0.1 形式的IP地址转换成10进制整数,这里没有进行任何错误处理 * * @param strIP * @return */ public static long ipToLong(String strIP) { long[] ip = new long[4]; int position1 = strIP.indexOf("."); int position2 = strIP.indexOf(".", position1 + 1); int position3 = strIP.indexOf(".", position2 + 1); 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)); long value = (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; return value; // ip1*256*256*256+ip2*256*256+ip3*256+ip4 } /** * 将10进制整数形式转换成127.0.0.1形式的IP地址 * @param longIP * @return */ public static String longToIP(long longIP) { StringBuffer sb = new StringBuffer(""); sb.append(String.valueOf(longIP >>> 24));// 直接右移24位 sb.append("."); sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16)); // 将高8位置0,然后右移16位 sb.append("."); sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8)); sb.append("."); sb.append(String.valueOf(longIP & 0x000000FF)); return sb.toString(); } /** * 根据子网网络位数计算子网掩码(2进制) * @param num * @return String */ public static String SubNumToBinSubMask(int num) { StringBuffer sb = new StringBuffer(); int location = 0; for (; num > 0; num--, location++) { sb.append(String.valueOf(1)); } for (; location < Num; location++) { sb.append(String.valueOf(0)); } return sb.toString(); } /** * 根据2进制IP地址得到点分十进制表示IP地址 * @param ipAddress * @return String */ public static String BinIPToLong(String ipAddress) { StringBuffer sb = new StringBuffer(); Long ip = Long.parseLong(ipAddress, 2); return longToIP(ip); } /** * 根据二进制IP地址、子网掩码 获得二进制子网地址 * @param iplist 二进制IP地址 * @param sublist 二进制子网掩码 * @return String 点分十进制子网掩码 */ public static String getSubnetAddress(char[] iplist,char[] sublist) { StringBuffer sb = new StringBuffer(""); int value = 0; int length = (iplist.length>sublist.length)?sublist.length:iplist.length; int i = 0; int j = Math.abs(iplist.length-sublist.length); for (; i < length; i++,j++) { value = Integer.parseInt(String.valueOf(iplist[i]))& Integer.parseInt(String.valueOf(sublist[j])); sb.append(value); } return BinIPToLong(sb.toString()); } /** * 根据IP地址(点分十进制)、子网网络位数获得子网地址 * @param num * @param ipAddress IP地址(点分十进制) * @return String 点分十进制子网 */ public static String getSubnetAddress(int num,String ipAddress) { String binSubIPAddress = SubNumToBinSubMask(num); String ip = Long.toBinaryString(ipToLong(ipAddress)); StringBuilder sb = new StringBuilder(ip); return getSubnetAddress(sb.toString().toCharArray(),binSubIPAddress.toCharArray()); } /** * 根据IP地址(十进制)、子网网络位数获得子网地址 * @param num 子网网络位数 * @param ipAddress IP地址(十进制) * @return String 点分十进制子网 */ public static String getSubnetAddress(int num,long ipAddress) { String binSubIPAddress = SubNumToBinSubMask(num); String ip = Long.toBinaryString(ipAddress); StringBuilder sb = new StringBuilder(ip); return getSubnetAddress(sb.toString().toCharArray(),binSubIPAddress.toCharArray()); } /** * 根据子网地址(十进制)、子网网络位数获得网段最后的IP地址 * @param num * @param ip * @return String 点分十进制子网 */ public static String getEndSubnetAddress(int num,String ip) { String ipAddress = Long.toBinaryString(ipToLong(ip)); StringBuilder sb = new StringBuilder(); for(int i=0;i<Num-ipAddress.length();i++){ sb.append("0"); } sb.append(ipAddress); char[] valuelist = sb.toString().toCharArray(); for(int k=num;k<Num && k<valuelist.length;k++) { valuelist[k]='1'; } return BinIPToLong(String.valueOf(valuelist)); } /** * 根据子网地址(十进制)、子网网络位数获得网段最后的IP地址 * @param num 子网网络位数 * @param ip 子网地址(十进制) * @return String 点分十进制 */ public static String getEndSubnetAddress(int num,long ip) { String ipAddress = Long.toBinaryString(ip); StringBuilder sb = new StringBuilder(); for(int i=0;i<Num-ipAddress.length();i++){ sb.append("0"); } sb.append(ipAddress); char[] valuelist = sb.toString().toCharArray(); for(int k=num;k<Num && k<valuelist.length;k++) { valuelist[k]='1'; } return BinIPToLong(String.valueOf(valuelist)); } public static void main(String[] args) { } }