The method of transforming IP and integer in Java

Source: Internet
Author: User
Tags integer split stringbuffer

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

Via left shift (<<), with (&), or (|) These operations are converted to int

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)

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 143 144 145 146 147 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)) &amp; 0xFF); RET[1] = (byte) (Integer.parseint (iparr[1)) &amp; 0xFF); RET[2] = (byte) (Integer.parseint (iparr[2)) &amp; 0xFF); RET[3] = (byte) (Integer.parseint (iparr[3)) &amp; 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] &amp; 0xFF). Append ('. '). Append (bytes[1] &amp; 0xFF). Append ('. '). Append (bytes[2] &amp; 0xFF). Append ('. '). Append (bytes[3] &amp; 0xFF). toString (); public static int bytestoint (byte[] bytes) {int addr = bytes[3] &amp; 0xFF addr |= ((bytes[2) &lt;&lt; 8) &amp; 0XFF00 ); Addr |= (bytes[1] &lt;&lt;) &amp; 0xff0000); Addr |= (Bytes[0] &lt;&lt;) &amp; 0xff000000); return addr; public static int Iptoint (String ipaddr) {try {return Bytestoint (iptobytesbyinet (IPADDR));} catch (Exception e) {THR ow new IllegalArgumentException (ipaddr + "is invalid IP"); } public static byte[] Inttobytes (int ipint) {byte[] ipaddr = new Byte[inaddrsz]; ipaddr[0] = (byte) (Ipint &GT;&GT;&G T ) &amp; 0xFF); IPADDR[1] = (byte) ((Ipint &gt;&gt;&gt;) &amp; 0xFF); IPADDR[2] = (byte) ((Ipint &gt;&gt;&gt; 8) &amp; 0xFF); IPADDR[3] = (byte) (Ipint &amp; 0xFF); return ipaddr; public static String Inttoip (int ipint) {return new StringBuilder (). Append ((Ipint &gt;&gt;) &amp; 0xff). Append ('. ') ). Append ((Ipint &gt;&gt;) &amp; 0xff). Append ('. '). Append ((Ipint &gt;&gt; 8) &amp; 0xff). Append ('. '). Append (ipint; 0xff)). ToString (); public static int[] Getipintscope (String ipandmask) {string[] Iparr = Ipandmask.split ("/"); if (Iparr.length!= 2) {th Row new IllegalArgumentException ("Invalid ipandmask with:" + Ipandmask);} int netMask = integer.valueof (Iparr[1].trim ()); if (NetMask &lt; 0 | | NetMask &gt; To) {throw new IllegalArgumentException ("Invalid ipandmask with:" + Ipandmask);} int ipint = Ipv4util.iptoint (iparr[0]); int netip = ipint &amp; (0xFFFFFFFF &lt;&lt; (32-netmask)); int hostscope = (0xFFFFFFFF &gt;&gt;&gt; netMask); return new int[] {netip, netip + hostscope}; public static string[] Getipaddrscope (String ipandmask) {int[] Ipintarr = Ipv4util.getipintscope (Ipandmask); 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 &amp; 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.iptob Ytesbyinet (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--&gt; 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--&gt; byte[]: [" + Bytestr +] "); System.out.println ("byte[]:" + bytestr + "--&gt; IP:" + Ipv4util.bytestoip (Bytearr)); int ipint = Ipv4util.iptoint (ipaddr); System.out.println ("IP:" + ipaddr + "--&gt; int:" + ipint); SYSTEM.OUT.PRINTLN ("int:" + ipint + "--&gt; IP:" + Ipv4util.inttoip (ipint)); String ipandmask = "192.168.1.1/24"; int[] Ipscope = Ipv4util.getipintscope (Ipandmask); System.out.println (Ipandmask + "--&gt; int address segment: [" + ipscope[0] + "," + ipscope[1] + "]); System.out.println (Ipandmask + "--&gt; 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 + "--&gt; int address segment: [" + ipscope1[0] + "," + ipscope1[1] + "]); System.out.println (IpAddr1 + "," + IpMask1 + "--&gt; IP address segment: [" + Ipv4util.inttoip (ipscope1[0]) + "," + Ipv4util.intto Ip (ipscope1[1]) + "]"); } }

I hope this article will help you with your Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.