任意(2-36)进制互转

本文深入探讨了一种高效的进制转换算法,通过实例演示了如何将不同进制的数字进行相互转换,包括从源进制到目标进制的转换过程。文章详细解释了算法的工作原理,包括数字解析、基数转换及结果格式化,为理解复杂的数据处理提供了清晰的视角。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.HashMap;
import java.util.Map;

public class Number {
    public static Character[] format = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    public static Map<Character, Integer> formatMap = new HashMap<Character, Integer>(format.length);

    static {
        for (int i = 0; i < format.length; i++) {
            formatMap.put(format[i], i);
        }
    }

    public static void main(String[] args) {
        try {
            String transfor = transfor("11k01.", 2, 10);
            System.out.println("转换后的值为:" + transfor);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }

    public static String transfor(String value, int source, int destination) throws IllegalArgumentException {
        if (value == null || value.trim().length() == 0 || source < 2 || source > 36 || destination < 2 || destination > 36) {
            throw new IllegalArgumentException(String.format("参数有误,请检查参数是否正确(value不能为空,2 <= source <= 36,2 <= destination <= 36).\n您的输入为:\nvalue = \"%s\"\nsource = %d\ndestination = %d", value, source, destination));
        }
        value = value.trim();
        if (source == destination) {
            return value;
        }
        int num = 0;
        if (source == 10) {
            num = Integer.parseInt(value);
        } else {
            int length = value.length();
            for (int index = 0; index < length; index++) {
                Integer charValue = formatMap.get(value.charAt(length - 1 - index));
                if (charValue != null && charValue < source) {
                    num += charValue * Math.pow(source, index);
                } else {
                    throw new IllegalArgumentException(String.format("入参\"%s\"不是%d进制数", value, source));
                }
            }
        }

        System.out.println("十进制数为:" + num);
        if (destination == 10) {
            return String.valueOf(num);
        }
        StringBuilder sb = new StringBuilder();
        while (num > 0) {
            int modResult = num % destination;
            num = num / destination;
            sb.append(format[modResult]);
        }
        StringBuilder result = new StringBuilder();
        int sbLength = sb.length();
        for (int index = 0; index < sbLength; index++) {
            result.append(sb.charAt(sbLength - 1 - index));
        }
        return result.toString();
    }


    public static String transforGo(String value, int source, int destination) throws IllegalArgumentException {
        if (value == null || value.trim().length() == 0 || source < 2 || source > 36 || destination < 2 || destination > 36) {
            throw new IllegalArgumentException(String.format("参数有误,请检查参数是否正确(value不能为空,2 <= source <= 36,2 <= destination <= 36).\n您的输入为:\nvalue = \"%s\"\nsource = %d\ndestination = %d", value, source, destination));
        }
        value = value.trim();
        if (source == destination) {
            return value;
        }
        int num = 0;
        return null;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值