publicclassintIp{ publicstaticvoidmain(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;
publicclassintIp{ publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); long d = Long.parseLong(sc.nextLine());