IP dotted decimal string conversion between integer and 32-bit Java implementation int
Relatively simple basic question, can record and review basis.
IPv4 (4 * 8) and int 32-bit integer can just map 11, but is signed int integer, it is not directly divided into four by an integer, and then multiplied by the weight (256 ^ (0-3)) and then adding it to the system conversion, the process will be int integer overflow. If it is converted to a long-type inverted IPv4 do not consider this issue.
Here directly realized by a simple bit operation, the first java, then mark the hexadecimal notation (cited java hexadecimal ):
System.out.println(0b101);//二进制:5 (0b开头的)
System.out.println(0e1011);//0.0
System.out.println(011);//八进制:9 (0开头的)
System.out.println(11);//十进制:11
System.out.println(0x11C);//十六进制:284 (0x开头的)
System.out.printf("%010x\n",7);//0000000007 按10位十六进制输出,向右靠齐,左边用0补齐
System.out.printf("%010o\n",13);//0000000015 按10位八进制输出,向右靠齐,左边用0补齐
System.out.printf("%x\n",7);//7 按16进制输出
System.out.printf("%o\n",13);//15 按8进制输出
System.out.println(Integer.toBinaryString(11));//1011 二进制
The code is implemented as:
public class TransIPtoInt
{
private int iPToInt(String ip) throws Exception
{
ip = ip.trim();
String regular = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
String[] iparray = ip.split("\\.");
if (!ip.matches(regular) || iparray.length != 4)
{
throw new Exception("Wrong IP.");
}
return Integer.parseInt(iparray[0]) << 24 | Integer.parseInt(iparray[1]) << 16
| Integer.parseInt(iparray[2]) << 8 | Integer.parseInt(iparray[3]);
}
private String intToIP(int ipnum)
{
return (int) ((ipnum & 0xff000000L) >> 24) + "." + (int) ((ipnum & 0xff0000L) >> 16) + "."
+ (int) ((ipnum & 0xff00L) >> 8) + "." + (int) (ipnum & 0xffL);
}
public static void main(String[] args) throws Exception
{
TransIPtoInt tp = new TransIPtoInt();
String ip = "232.132.72.255";
int ipnum = tp.iPToInt(ip);
System.out.println(ipnum);
System.out.println(tp.intToIP(ipnum));
}
}