The example of this article tells the method of IP and integer in Java. Share it for everyone for your reference. The specific analysis is as follows:
1. Basic knowledge points
IP-> integer:
Convert the IP address into byte array through the left shift (<<), (&), or (|) these operations to int to int
Integer-> IP:
Right shift operation (>>>), shift the right to 24 bits, and then perform the operator (&) 0xff. The obtained number is the first IP.
Right shift operation (>>>), shift 16 bits right, and then perform the operator (&) 0xff. The number obtained is the second IP.
Right shift operation (>>>), move to the right of 8 bits, and then perform the operator (&) 0xff. The number obtained is the third segment IP.
The integer value is performed with the operator (&) 0xff, and the number obtained is the fourth stage 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 [] iptobytes Byinet (string ipaddr) {try {return inbetdress.getByname (ipaddr) .getaddress ();} Catch (Exception E) {Throw New iLleGALARGUMENTEXCEPTION (ipaddr + "is invalid ip"); (String iPaddr) {byte [] Ret = New Byte [4]; TRY {String [] iparr = ipaddr.split ("//."); RET [0] = (byte) (Integer.parseint (iparr [0]); RET [1] = (byte) (Integer. Parseint (iparr [1]) & 0xff; RET [2] = (byte) (Integer.parseint (iparr [2]); RET [3] = (byte) (Iparr [3] ) & 0XFF); Return Ret;} Catch (Exception E) {Throw New IlLegalargumentexception (iPaddr + "is invalid ip"); Te [] bytes) {Return New StringBuffer (). Append (bytes [0] & 0XFF) .append ('.'). Append (bytes [1] & 0xff) .appnd ('.'). Append (bytes [2] & 0xff) .appnd ('.'). bytes [3] & 0XFF) .tring ();} Public Static IntoStoint (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 iPaddr (string iPaddr) {try {try {testr). Return Bytestoint (iptobytesByinet (ipaddr));} Catch (Exception E) {Throw New illegalargumexception (ipaddr + "is invalid ip"); Atic byte [] intBytes (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 NewBuilder () .append ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( ipint >> 24) & 0XFF). Append ('.') .appnd ((ipint >> 16) & 0xff) .appnd ('.'). '.'). Append ((ipInt & 0XFF)) .Tostring ();} Public Static int [] Getipintscope (String iPandmask) {String [] ipArr = ipandmask.split ("/"); if R.Length! = 2) {Throw New illegalargumenTexception ("Invalid iPandmask with:" + iPandmask);} int netMask = Integer.valueof (iparr [1] .trim ()); if (netma SK <0 || Netmask> 31) {Throw New IllegalargumenTexception ("Invalid iPandmask with:" + iPandMask);} int iPint = ipv4util.iptoint (iparr [0]); int NETIP = ipint & (0xFFFFFFFFFFF <(3 3 2 -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, IPC OUNT = 0; TRY {ipint = ipv4util. IPTOINT (ipaddr); if (null == mask || ".equals (mask)) {Return New into [] {ipint, ipint};} NetMaskint = ipv4util.iptoint (Mask); ipcount = Ipv4util.ips (" 255.255.255.255 ") -NETMASKINT; int Netip = ipInt & NetMaskint; int HostScope = Netip + ipcount; Return New Int [] {network, hostScope};} H (Exception E) {Throw New iLlegalargumentedException ("Invalid IP Scope Express IP : " + iPaddr +" Mask: " + Mask);}} Public Static String [] GetipStrscope (String iPaddr, String Mask) {int [] ipv4util.GetipintScope (IP Addr, 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 + "byte-> byte []: [" + bytestr + "]); bytearr = ipv4util.iptobyreg (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.ips (iPaddr); system.out.println (" ip: " + ipaddr +" -> " + ipint); System.out .println ( "Int: + IPint +" -> IP: " + IPv4util.inttoip (ipint)); String iPandmask =" 192.168.1.1/24 "; int [] ipscope = ipv4util.getIPINTSCOPE (ipandmask );; Println (ipandmask + " -> int address segment: [" + ipscope [0] + "," + ipscope [1] + "]"); System.out.println (ipandmask + "-> IP address segment: [ + 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 address: [" + ipscope1 [0] + "," + + "," IPSCOPE1 [1 ] + "]); System.out.println (ipaddr1 +", " + ipmask1 +" -> IP address segment: [" + ipv4util.inttoip (IPSCOPE1 [0]) +", " + ipv4util.inttoip ( IPSCOPE1 [1]) + "]" ");}}
It is hoped that this article is helpful to everyone's Java program design.