• 10进制转任意进制,任意进制转10进制


    系统需要实现任意10进制和任意进制之间的转化(36进制以内),任意进制转化为10进制 10进制转换为任意进制
    
    package com.myhexin.bootdemo.level3;
    
    import java.util.Stack;
    
    /**
     * 进制数字转化
     *
     * @author qiaolei
     * @date 2022/09/16
     */
    public class SwitchNumber {
        public static void main(String[] args) {
            int src = 100;
            int toBase = 36;
            String outPutStr = "";
            outPutStr = Base10ToBaseN(src, toBase, outPutStr);
            ;
    
            System.out.println(outPutStr);
            int outPutStr2=0;
            outPutStr2=BaseNtoBase10("1A1",36);
            System.out.println(outPutStr2);
        }
    
        public static String Base10ToBaseN(int src, int toBase, String outPutStr) {
            String chs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            StringBuffer sb = new StringBuffer();
            String digths = chs.substring(0, toBase);
            Stack s = new Stack<>();
            while (src != 0) {
                s.push(digths.charAt(src % toBase));
                src /= toBase;
            }
            while (!s.isEmpty()) {
    
                sb.append(s.pop());
    
            }
            outPutStr = sb.toString();
            return outPutStr;
        }
    
        /**
         * n进制转化为10进制
         *
         * @param planstr n 进制字符串
         * @return iBase 表示几进制
         */
    
        public static int BaseNtoBase10(String planstr, int iBase) {
            int outPutStr = 0;
            int temp = 0;
            int d;
            int p = 0;
            char c;
            int length = planstr.length();
            for (int i = 0; i < length; i++) {
                c = planstr.charAt(i);
                //将数字转化为对应的数字
                if (c >= 'A' && c <= 'Z') {
                    d = c - 55;
                } else if (c >= 'a' && c <= 'Z') {
                    d = c - 87;
                } else {
                    d = c - 48;
                }
    
                //当前位权
    
                p = length - 1 - i;
                //0^0=1
                if (d != 0) {
                    temp += d * (int) Math.pow(iBase, p);
                }
            }
            outPutStr = temp;
            return outPutStr;
        }
    }
    
  • 相关阅读:
    NumPy 随机数据分布与 Seaborn 可视化详解
    大数据-Hadoop部署模式
    ARM编程环境搭建教程
    vue2技能树(5)-条件渲染和列表渲染
    C++ 新特性 | C++ 11 | 移动语义与右值引用
    笔记本电脑主板电池没电如何解决
    数据库 备份和恢复
    AI伦理:科技发展中的人性之声
    计算机毕业论文选题推荐|软件工程|系列八
    每日十(?)题之20220903
  • 原文地址:https://blog.csdn.net/QL753123/article/details/126910387