题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
import java.util.*; public class Main { public static void main(String[] args) { Scanner fzhinput = new Scanner(System.in); String srip = fzhinput.nextLine(); Long sjzip = fzhinput.nextLong(); String ip[] = srip.split("\\."); int swip[] = new int [ip.length]; StringBuilder stack = new StringBuilder(); StringBuilder stack1 = new StringBuilder(); for(int i=0;i<ip.length;i++){ swip[i]=Integer.parseInt(ip[i]); String s = String.format("%8s",Integer.toBinaryString(swip[i])).replace(" ","0"); stack.append(s); } String zfc = stack.toString(); Long zhh1=Long.parseLong(zfc,2); System.out.println(zhh1); String erzhh2 = String.format("%32s",Long.toBinaryString(sjzip)).replace(" ","0"); for(int i=1;i<=erzhh2.length();i++){ stack1.append(erzhh2.charAt(i-1)); if(i%8==0){ stack1.append(" "); } } String store[] = stack1.toString().split(" "); stack1.setLength(0); for(int i=0;i<store.length;i++){ stack1.append(Integer.parseInt(store[i],2)); if(i<store.length-1){ stack1.append("."); } } System.out.println(stack1.toString()); } }