题解 | #整数与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());
    }
}

全部评论

相关推荐

11-29 11:21
门头沟学院 Java
点赞 评论 收藏
分享
面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务