第一版答案:
package May16;
import java.util.*;
public class Demo12 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String ip = in.next();
String address = in.next();
System.out.println(ipToAddress(ip));
System.out.println(addressToIp(address));
}
}
public static String addressToIp(String address) {
Integer integer = Integer.valueOf(address);
String str = Integer.toBinaryString(integer);// 转换成了二进制形式00000100101010
int zero = 32 - str.length();
for (int i = 0; i < zero; i++) {
str = '0' + str;
}
String[] arr = new String[4];
arr[0] = str.substring(0, 8);
arr[1] = str.substring(8, 16);
arr[2] = str.substring(16, 24);
arr[3] = str.substring(24, 32);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
// 每个8位二进制要转换成10进制
int sum = 0;
for (int j = 0; j < 8; j++) {
if (arr[i].charAt(j) == '1') {
sum += (int) Math.pow(2, (7 - j));
}
}
sb.append(sum);
if (i != 3)
sb.append(".");
}
return sb.toString();
}
public static int ipToAddress(String ip) {
String[] arr = ip.split("\\.");
int[] arrInt = new int[4];
for (int i = 0; i < 4; i++) {
arrInt[i] = Integer.valueOf(arr[i]);
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
String str = Integer.toBinaryString(arrInt[i]);// 转换成了000001111形式
int zero = 8 - str.length();
for (int j = 0; j < zero; j++) {
str = "0" + str;
}
sb.append(str);
}
// 合并了。0000100100010101010的形式
String binary = sb.toString();
int res = 0;
int count = 0;
for (int i = binary.length() - 1; i >= 0; i--) {
if (binary.charAt(i) == '1')
res += (int) Math.pow(2, count);
count++;
}
return res;
}
}
第二版答案:将第一版的int换成了long
为什么呢?int是4字节的。范围为-231~231-1。假设你的ip地址为255.255.255.255 那么需要将1111…1111一共32个1组成的二进制数转成十进制。已经超出int的范围了。所以要用long来代替int。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String ip = in.next();
String address = in.next();
System.out.println(ipToAddress(ip));
System.out.println(addressToIp(address));
}
}
public static String addressToIp(String address) {
Long integer = Long.valueOf(address);
String str = Long.toBinaryString(integer);// 转换成了二进制形式00000100101010
int zero = 32 - str.length();
for (int i = 0; i < zero; i++) {
str = '0' + str;
}
String[] arr = new String[4];
arr[0] = str.substring(0, 8);
arr[1] = str.substring(8, 16);
arr[2] = str.substring(16, 24);
arr[3] = str.substring(24, 32);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
// 每个8位二进制要转换成10进制
int sum = 0;
for (int j = 0; j < 8; j++) {
if (arr[i].charAt(j) == '1') {
sum += (int) Math.pow(2, (7 - j));
}
}
sb.append(sum);
if (i != 3)
sb.append(".");
}
return sb.toString();
}
public static long ipToAddress(String ip) {
String[] arr = ip.split("\\.");
int[] arrInt = new int[4];
for (int i = 0; i < 4; i++) {
arrInt[i] = Integer.valueOf(arr[i]);
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
String str = Integer.toBinaryString(arrInt[i]);// 转换成了000001111形式
int zero = 8 - str.length();
for (int j = 0; j < zero; j++) {
str = "0" + str;
}
sb.append(str);
}
// 合并了。0000100100010101010的形式
String binary = sb.toString();
long res = 0;
int count = 0;
for (int i = binary.length() - 1; i >= 0; i--) {
if (binary.charAt(i) == '1')
res += (long) Math.pow(2, count);
count++;
}
return res;
}
}