JAVA---N进制转10进制,10进制转N进制

浪尽此生 提交于 2020-03-07 19:52:15

// K进制转10进制
    public static int DCHANGE(String NUM, int K) {
        int NUM_len = NUM.length();
        int total = 0;
        while (NUM_len > 0) {
            NUM_len -= 1;
            total += Integer.parseInt(NUM.charAt(NUM_len) + "")
                    * Math.pow(K, NUM_len);
        }
        return total;
    }

    // 10进制转K进制
    public static String RCHANGE(int NUM, int K) {
        String total = "";
        if (NUM == 0) {
            total = "0";
        } else {
            while (NUM > 0) {
                String temp = NUM % K + "";
                total = temp + total;
                NUM /= K;
            }
        }
        return total;
    }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!