Java实现八进制正整数转化为十进制数


public class Main {
    public static void main(String[] args) {
        System.out.println(convert(23,2));
//        System.out.println(power(8,2));
    }

    private static int convert(int n, int i) {//n为输入的八进制数,i为位数
        int temp[] = new int[i];
        int result = 0;
        for(int j=0;j < i;j++){
            temp[j] = (n/power(10,i-j-1))%power(10,1);
            result += temp[j]*power(8,i-j-1);
//            System.out.print(n + " " + temp[j] + " " + power(10,i-j-1) + " " + power(10,j) + "\n");
        }
//        System.out.println();
        return result;
    }

    private static int power(int i, int j) {
        int temp = 1;
        for(int k=0;k<j;k++){
            temp *= i;
        }
        return temp;
    }
}

 对于输入的一个八位数,取得每一位的数字,例如:3254/1000%10 = 3(前面的/为取整,后面的%为取余数,注意为取得每位上的数字,取得整数部分后,然后取每个整数的个位数就可以了,也就是%10)。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM