【牛客网】整数与IP地址间的转换

网址链接

在这里插入图片描述
在这里插入图片描述

第一版答案:

在这里插入图片描述

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;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值