题目描述

参照:整数与IP地址间的转换__牛客网

IP地址转整数

法一

注:Java中需要Long型,int会溢出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

public class ipInt {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String strIp = sc.nextLine();
String[] ipArr = strIp.split("\\.");

Long n = Long.parseLong(ipArr[0]);

for (int i = 1; i < 4; i++) {
n = n << 8; // 位运算将每一位往左移8
n += Long.parseLong(ipArr[i]);
}

System.out.println(n);
}
}

法二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;

public class ipInt {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String strIp = sc.nextLine();
String[] ipArr = strIp.split("\\.");

Long[] arr = new Long[4];
for (int i = 0; i < 4; i++) {
arr[i] = Long.parseLong(ipArr[i]);
}

Long res = 0L;
res += arr[0] << 24;
res += arr[1] << 16;
res += arr[2] << 8;
res += arr[3];
// 上述运算等同于一句话:
// res = arr[0] << 24 | arr[1] << 16 | arr[2] << 8 | arr[3];

System.out.println(res);
}
}

法三

数字转32位2进制,最前面补0,然后分成4个8位2进制拼起来一个长字符串,再转10进制加上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;

public class ipInt {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String strIp = sc.nextLine();
String[] ipArr = strIp.split("\\.");

long[] ip = new long[4];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
ip[i] = Long.parseLong(ipArr[i]);
String a = Long.toBinaryString(ip[i]);
//转成8位二进制 利用String.format()控制格式
String temp = String.format("%08d", Long.parseLong(a));
sb.append(temp);
}

long output = Long.parseLong(sb.toString(), 2); // radix参数指定了二进制
// valueOf()也行
System.out.println(output);
}
}

整数转IP地址

法一

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
import java.util.Scanner;

public class intIp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long strNum = Long.parseLong(sc.nextLine());

String strIp = Long.toBinaryString(strNum);
// 要在前面补足0
StringBuilder sb = new StringBuilder();
if (strIp.length() < 32) {
for (int i = 0; i < (32 - strIp.length()); i++) {
sb.append(0);
}
sb.append(strIp);
} else if (strIp.length() == 32) {
sb.append(strIp);
}

for (int i = 0; i < sb.length() - 8; i += 8) {
System.out.print(Integer.parseInt(sb.substring(i, i + 8), 2) + ".");
}

System.out.println(Integer.parseInt(sb.substring(sb.length() - 8, sb.length()), 2));
}
}

法二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;

public class intIp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long strNum = Long.parseLong(sc.nextLine());

String strIp = Long.toBinaryString(strNum);
int len = strIp.length();
StringBuilder sb = new StringBuilder(strIp);
// 不足32位的前面补0
for (int i = 0; i < 32 - len; i++) {
sb.insert(0, "0");
}
String fin = sb.toString();
long a = Long.valueOf(fin.substring(0, 8), 2);
long b = Long.valueOf(fin.substring(8, 16), 2);
long c = Long.valueOf(fin.substring(16, 24), 2);
long d = Long.valueOf(fin.substring(24, 32), 2);
System.out.println(a + "." + b + "." + c + "." + d);
}
}

法三

1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner;

public class intIp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long d = Long.parseLong(sc.nextLine());

String out = String.format("%d.%d.%d.%d", (d >> 24) & 0xff, (d >> 16) & 0xff, (d >> 8) & 0xff, d & 0xff);
System.out.println(out);
}
}