欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-04 20:35:42
本文实例讲述了java中ip和整数相互转化的方法。分享给大家供大家参考。具体分析如下: 一、基本知识点 ip ——> 整数: 把ip地址转化为字节数组 通过左...

本文实例讲述了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程序设计有所帮助。