This article illustrates the method of transforming IP and integer in java. Share to everyone for your reference. The specific analysis is as follows:
I. Basic points of knowledge
ip--> integer:
Converts an IP address into an array of bytes
Convert to int by left shift (<<), with (&), or (|)
Integer--> IP:
The integer value is shifted to the right (>>>), 24 digits to the right, and the operator (&) 0xFF, and the resulting number is the first segment of IP.
The integer value is shifted to the right (>>>), 16 digits to the right, and the operator (&) 0xFF, and the resulting number is the second segment of IP.
The integer value is shifted to the right (>>>), 8 digits to the right, and the operator (&) 0xFF, and the resulting number is the third segment of IP.
The integer value is performed with the operator (&) 0xFF, and the resulting number is the fourth segment IP.
Second, Java code example (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 Practice: 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] <<) & 0xff0000);
Addr |= (Bytes[0] <<) & 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 >>>) & 0xFF);
IPADDR[1] = (byte) ((Ipint >>>) & 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 >>) & 0xff). Append ('. ') . Append ((Ipint >>) & 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 > To) {throw new IllegalArgumentException ("Invalid ipandmask with:" + IPANDMA
SK);
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 (ipAd
DR, 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 address segment: [" + ipscope[0] + "," + ipscope[1] + "]); System.out.println (Ipandmask + "--> IP address segment: [" + Ipv4util.inttoip (ipscope[0]) + "," + Ipv4util.inttoip (Ipsco
PE[1]) + "]");
String IPADDR1 = "192.168.1.1", IpMask1 = "255.255.255.0";
int[] Ipscope1 = Ipv4util.getipintscope (IPADDR1, IpMask1);
System.out.println (IpAddr1 + "," + IpMask1 + "--> int address segment: [" + ipscope1[0] + "," + ipscope1[1] + "]); System.out.println (IpAddr1 + "," + IpMask1 + "--> IP address segment: [" + Ipv4util.inttoip (ipscope1[0]) + "," + ipv4u
Til.inttoip (ipscope1[1]) + "]"); }
}
I hope this article will help you with your Java programming.