192 x 256^3 + 168 x 256^2 + 1 x 256^1 + 1 x 1
3221225472 + 11010048 + 256 + 1 = 3232235777
http://3232235777用十进制数表示 IP 地址的两种方法:
![]() |
以二进制计算 (1 x 256^1) 的示例 |
public class IP_Decimale {Output
public static void main(String[] zero) {
String ip = 192.168.1.1”;
//十进制IP地址
long iplong = IPenBase10(ip);
System.out.println(iplong);
}
public static long IPenBase10(String IP address) {
long decimal = 0;
//数字用点分隔
//把每个数字放在数组的框里
String[] arrayIP = IP.address.split(\\.”);
//从右到左
for (int i = 3; i >= 0; i--) {
//转换为 long
long ip = Long.parseLong(arrayIP[3 - i]);
//向左移动 (i*8) 位,sum
long bitshift= ip <<(i*8);
十进制 += 位移;
System.out.println(ip+ <<+(i*8)+” : +位移);
}
返回十进制;
}
}
192 <<24 : 3221225472
168 <<16 : 11010048
1 <<8 : 256
1 <<0 : 1
result: 3232235777
public static long IPenBase10puiss(String IP address) {参考:
long decimal = 0;
String[] arrayIP = IP.address.split(\\.”);
//从右到左
for (int i = 0; i<=3; i++) {
long ip = Long.parseLong(arrayIP[i]);
long puiss= ip * (long) Math.pow(256, 3-i);
十进制 += 幂;
System.out.println(arrayIP[i] +: ”+puiss);
}
返回十进制;
}
Please disable your ad blocker and refresh the window to use this website.