JAVA中IP和整数相互转化的方法

 更新时间:2015年05月29日 15:06:42   作者:wo_soul  
这篇文章主要介绍了JAVA中IP和整数相互转化的方法,涉及java数值转换的相关技巧,需要的朋友可以参考下

本文实例讲述了JAVA中IP和整数相互转化的方法。分享给大家供大家参考。具体分析如下:

一、基本知识点

IP ——> 整数:
把IP地址转化为字节数组
通过左移位(<<)、与(&)、或(|)这些操作转为int
整数 ——> IP:
将整数值进行右移位操作(>>>),右移24位,再进行与操作符(&)0xFF,得到的数字即为第一段IP。
将整数值进行右移位操作(>>>),右移16位,再进行与操作符(&)0xFF,得到的数字即为第二段IP。
将整数值进行右移位操作(>>>),右移8位,再进行与操作符(&)0xFF,得到的数字即为第三段IP。
将整数值进行与操作符(&)0xFF,得到的数字即为第四段IP。

二、java代码示例(IPv4Util.java)

package michael.utils; 
import java.net.InetAddress; 
public class IPv4Util { 
 private final static int INADDRSZ = 4; 
 public static byte[] ipToBytesByInet(String ipAddr) { 
  try { 
   return InetAddress.getByName(ipAddr).getAddress(); 
  } catch (Exception e) { 
   throw new IllegalArgumentException(ipAddr + " is invalid IP"); 
  } 
 }JTA实践:Spring+ATOMIKOS 
 public static byte[] ipToBytesByReg(String ipAddr) { 
  byte[] ret = new byte[4]; 
  try { 
   String[] ipArr = ipAddr.split("\\."); 
   ret[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF); 
   ret[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF); 
   ret[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF); 
   ret[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF); 
   return ret; 
  } catch (Exception e) { 
   throw new IllegalArgumentException(ipAddr + " is invalid IP"); 
  } 
 } 
 public static String bytesToIp(byte[] bytes) { 
  return new StringBuffer().append(bytes[0] & 0xFF).append('.').append( 
    bytes[1] & 0xFF).append('.').append(bytes[2] & 0xFF) 
    .append('.').append(bytes[3] & 0xFF).toString(); 
 } 
 public static int bytesToInt(byte[] bytes) { 
  int addr = bytes[3] & 0xFF; 
  addr |= ((bytes[2] << 8) & 0xFF00); 
  addr |= ((bytes[1] << 16) & 0xFF0000); 
  addr |= ((bytes[0] << 24) & 0xFF000000); 
  return addr; 
 } 
 public static int ipToInt(String ipAddr) { 
  try { 
   return bytesToInt(ipToBytesByInet(ipAddr)); 
  } catch (Exception e) { 
   throw new IllegalArgumentException(ipAddr + " is invalid IP"); 
  } 
 } 
 public static byte[] intToBytes(int ipInt) { 
  byte[] ipAddr = new byte[INADDRSZ]; 
  ipAddr[0] = (byte) ((ipInt >>> 24) & 0xFF); 
  ipAddr[1] = (byte) ((ipInt >>> 16) & 0xFF); 
  ipAddr[2] = (byte) ((ipInt >>> 8) & 0xFF); 
  ipAddr[3] = (byte) (ipInt & 0xFF); 
  return ipAddr; 
 } 
 public static String intToIp(int ipInt) { 
  return new StringBuilder().append(((ipInt >> 24) & 0xff)).append('.') 
    .append((ipInt >> 16) & 0xff).append('.').append( 
      (ipInt >> 8) & 0xff).append('.').append((ipInt & 0xff)) 
    .toString(); 
 } 
 public static int[] getIPIntScope(String ipAndMask) { 
  String[] ipArr = ipAndMask.split("/"); 
  if (ipArr.length != 2) { 
   throw new IllegalArgumentException("invalid ipAndMask with: " 
     + ipAndMask); 
  } 
  int netMask = Integer.valueOf(ipArr[1].trim()); 
  if (netMask < 0 || netMask > 31) { 
   throw new IllegalArgumentException("invalid ipAndMask with: " 
     + ipAndMask); 
  } 
  int ipInt = IPv4Util.ipToInt(ipArr[0]); 
  int netIP = ipInt & (0xFFFFFFFF << (32 - netMask)); 
  int hostScope = (0xFFFFFFFF >>> netMask); 
  return new int[] { netIP, netIP + hostScope }; 
 } 
 public static String[] getIPAddrScope(String ipAndMask) { 
  int[] ipIntArr = IPv4Util.getIPIntScope(ipAndMask); 
  return new String[] { IPv4Util.intToIp(ipIntArr[0]), 
    IPv4Util.intToIp(ipIntArr[0]) }; 
 } 
 public static int[] getIPIntScope(String ipAddr, String mask) { 
  int ipInt; 
  int netMaskInt = 0, ipcount = 0; 
  try { 
   ipInt = IPv4Util.ipToInt(ipAddr); 
   if (null == mask || "".equals(mask)) { 
    return new int[] { ipInt, ipInt }; 
   } 
   netMaskInt = IPv4Util.ipToInt(mask); 
   ipcount = IPv4Util.ipToInt("255.255.255.255") - netMaskInt; 
   int netIP = ipInt & netMaskInt; 
   int hostScope = netIP + ipcount; 
   return new int[] { netIP, hostScope }; 
  } catch (Exception e) { 
   throw new IllegalArgumentException("invalid ip scope express ip:" 
     + ipAddr + " mask:" + mask); 
  } 
 } 
 public static String[] getIPStrScope(String ipAddr, String mask) { 
  int[] ipIntArr = IPv4Util.getIPIntScope(ipAddr, mask); 
  return new String[] { IPv4Util.intToIp(ipIntArr[0]), 
    IPv4Util.intToIp(ipIntArr[0]) }; 
 } 
 public static void main(String[] args) throws Exception { 
  String ipAddr = "192.168.8.1"; 
  byte[] bytearr = IPv4Util.ipToBytesByInet(ipAddr); 
  StringBuffer byteStr = new StringBuffer(); 
  for (byte b : bytearr) { 
   if (byteStr.length() == 0) { 
    byteStr.append(b); 
   } else { 
    byteStr.append("," + b); 
   } 
  } 
  System.out.println("IP: " + ipAddr + " ByInet --> byte[]: [ " + byteStr 
    + " ]"); 
  bytearr = IPv4Util.ipToBytesByReg(ipAddr); 
  byteStr = new StringBuffer(); 
  for (byte b : bytearr) { 
   if (byteStr.length() == 0) { 
    byteStr.append(b); 
   } else { 
    byteStr.append("," + b); 
   } 
  } 
  System.out.println("IP: " + ipAddr + " ByReg --> byte[]: [ " + byteStr 
    + " ]"); 
  System.out.println("byte[]: " + byteStr + " --> IP: " 
    + IPv4Util.bytesToIp(bytearr)); 
  int ipInt = IPv4Util.ipToInt(ipAddr); 
  System.out.println("IP: " + ipAddr + " --> int: " + ipInt); 
  System.out.println("int: " + ipInt + " --> IP: " 
    + IPv4Util.intToIp(ipInt)); 
  String ipAndMask = "192.168.1.1/24"; 
  int[] ipscope = IPv4Util.getIPIntScope(ipAndMask); 
  System.out.println(ipAndMask + " --> int地址段:[ " + ipscope[0] + "," 
    + ipscope[1] + " ]"); 
  System.out.println(ipAndMask + " --> IP 地址段:[ " 
    + IPv4Util.intToIp(ipscope[0]) + "," 
    + IPv4Util.intToIp(ipscope[1]) + " ]"); 
  String ipAddr1 = "192.168.1.1", ipMask1 = "255.255.255.0"; 
  int[] ipscope1 = IPv4Util.getIPIntScope(ipAddr1, ipMask1); 
  System.out.println(ipAddr1 + " , " + ipMask1 + " --> int地址段 :[ " 
    + ipscope1[0] + "," + ipscope1[1] + " ]"); 
  System.out.println(ipAddr1 + " , " + ipMask1 + " --> IP地址段 :[ " 
    + IPv4Util.intToIp(ipscope1[0]) + "," 
    + IPv4Util.intToIp(ipscope1[1]) + " ]"); 
 } 
}

希望本文所述对大家的java程序设计有所帮助。

相关文章

  • Spring Boot 连接LDAP的方法

    Spring Boot 连接LDAP的方法

    这篇文章主要介绍了Spring Boot 连接LDAP的方法,仅仅涉及基本的使用ODM来快速实现LDAP增删改查操作。具有一定的参考价值,有兴趣的可以了解一下
    2017-12-12
  • Java多线程的实现方式比较(两种方式比较)

    Java多线程的实现方式比较(两种方式比较)

    Java多线程实现方式有两种,第一种是继承Thread类,第二种是实现Runnable接口,两种有很多差异,下面跟着本文一起学习吧
    2015-11-11
  • Java后端Cookie实现(时间戳)代码实例

    Java后端Cookie实现(时间戳)代码实例

    这篇文章主要介绍了Java后端Cookie实现(时间戳)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • 利用feign调用返回object类型转换成实体

    利用feign调用返回object类型转换成实体

    这篇文章主要介绍了利用feign调用返回object类型转换成实体,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Mybatis中使用updateBatch进行批量更新

    Mybatis中使用updateBatch进行批量更新

    这篇文章主要介绍了Mybatis中使用updateBatch进行批量更新的相关资料,有逐条更新,sql批量更新等,具体实例代码大家参考下本文
    2018-04-04
  • Springboot整合mybatis开启二级缓存的实现示例

    Springboot整合mybatis开启二级缓存的实现示例

    在一级缓存中,是查询两次数据库的,显然这是一种浪费,既然SQL查询相同,就没有必要再次查库了,直接利用缓存数据即可,这种思想就是MyBatis二级缓存的初衷,本文就详细的介绍了Springboot整合mybatis开启二级缓存,感兴趣的可以了解一下
    2022-05-05
  • JAVA中字符串函数subString的用法小结

    JAVA中字符串函数subString的用法小结

    本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-02-02
  • Springboot2.x+ShardingSphere实现分库分表的示例代码

    Springboot2.x+ShardingSphere实现分库分表的示例代码

    这篇文章主要介绍了Springboot2.x+ShardingSphere实现分库分表的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • SpringBoot 注解事务声明式事务的方式

    SpringBoot 注解事务声明式事务的方式

    springboot使用上述注解的几种方式开启事物,可以达到和xml中声明的同样效果,但是却告别了xml,使你的代码远离配置文件。今天就扒一扒springboot中事务使用注解的玩法,感兴趣的朋友一起看看吧
    2017-09-09
  • C语言中下标与指针的转换以及指向指针的指针的例子

    C语言中下标与指针的转换以及指向指针的指针的例子

    这篇文章主要介绍了C语言中下标与指针的转换以及指向指针的指针的示例,是C语言入门学习中的基础知识,需要的朋友可以参考下
    2015-11-11

最新评论