十进制转换二,八,十六进制 及 二,八,十六进制转十进制

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
/**
* 功能:十进制转换二,八,十六进制 及 二,八,十六进制转十进制
* */
public class JinZhiZhuanHuan {

static int Decimal=241;//十进制
static int Binary=11110001;//二进制
static int Octal=361;//八进制
static String Hexadecimal="F1";//十六进制
static List<String> Decimal_Binarylist =new ArrayList<String>();
static List<String> Decimal_Octallist =new ArrayList<String>();
static List<String> Decimal_Hexadecimallist =new ArrayList<String>();

public static void main(String[] args) {
    Decimal_All();//十进制转多进制
    All_Decimal();//多进制转十进制
}

public static void Decimal_All(){//十进制转多进制
    Decimal_Binary(Decimal);
    String D_B="";
    for(int i=Decimal_Binarylist.size()-1;i>=0;i--){
        D_B+=Decimal_Binarylist.get(i);
    }
    System.out.println("十进制数:"+Decimal+" 转换为二进制数是:"+D_B);
    System.out.println();

    Decimal_Octal(Decimal);
    String D_O="";
    for(int i=Decimal_Octallist.size()-1;i>=0;i--){
        D_O+=Decimal_Octallist.get(i);
    }
    System.out.println("十进制数:"+Decimal+" 转换为八进制数是:"+D_O);
    System.out.println();

    Decimal_Hexadecimal(Decimal);
    String D_H="";
    for(int i=Decimal_Hexadecimallist.size()-1;i>=0;i--){
        D_H+=Decimal_Hexadecimallist.get(i);
    }
    System.out.println("十进制数:"+Decimal+" 转换为十六进制数是:"+D_H);
    System.out.println();
}

public static void All_Decimal(){//多进制转十进制

    System.out.println("二进制数:"+Binary+" 转换为十进制数是:"+Binary_Decimal(Binary));
    System.out.println();

    System.out.println("八进制数:"+Octal+" 转换为十进制数是:"+Octal_Decimal(Octal));
    System.out.println();

    System.out.println("十六进制数:"+Hexadecimal+" 转换为十进制数是:"+Hexadecimal_Decimal(Hexadecimal));
    System.out.println();

}

public static void  Decimal_Binary(int num){//十进制转二进制
    String[] size={"0","1"};
    int a=num/2;
    int b=num%2;
    Decimal_Binarylist.add(size[b]);
    if(a>0){
        Decimal_Binary(a);
    }
}

public static void Decimal_Octal(int num){//十进制转八进制
    String[] size={"0","1","2","3","4","5","6","7"};
    int a=num/8;
    int b=num%8;
    Decimal_Octallist.add(size[b]);
    if(a>0){
        Decimal_Octal(a);
    }

}

public static void Decimal_Hexadecimal(int num){//十进制转十六进制
    String[] size={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F",};
    int a=num/16;
    int b=num%16;
    Decimal_Hexadecimallist.add(size[b]);
    if(a>0){
        Decimal_Hexadecimal(a);
    }
}

public static int Binary_Decimal(int num){//二进制转十进制
    String a = num+"";
    int c=0;
    for(int i=0;i<a.length();i++){
        String d=a.charAt(i)+"";
        int b = Integer.valueOf(d);
        if(b==0||b==1){
            c+=b*Math.pow(2, (a.length()-1-i));//BintoDec(a.length()-1-i);
        }else{
            JOptionPane.showMessageDialog(null, "输入的二进制数有误!");
            return 0 ;
        }
    }
    return c;
}

public static int Octal_Decimal(int num){//八进制转十进制
    String a = num+"";
    int c=0;
    for(int i=0;i<a.length();i++){
        String d=a.charAt(i)+"";
        int b =Integer.valueOf(d);
        if((b+"").matches("[0-7]")){
            c+=b*Math.pow(8, (a.length()-1-i));//OctaltoDec(a.length()-1-i);
        }else{
            JOptionPane.showMessageDialog(null, "输入的八进制数有误!");
            return 0 ;
        }
    }
    return c;
}

public static int Hexadecimal_Decimal(String num){//十六进制转十进制
    String a = num.trim();
    int c=0;
    int b =0;
    for(int i=0;i<a.length();i++){
        String d=a.charAt(i)+"";
        if(d.matches("[0-9]")||d.matches("[A-F]")){
                if(d.equals("A")){
                    b=10;
                }else if(d.equals("B")){
                    b=11;
                }else if(d.equals("C")){
                    b=12;
                }else if(d.equals("D")){
                    b=13;
                }else if(d.equals("E")){
                    b=14;
                }else if(d.equals("F")){
                    b=15;
                }else{
                     b =Integer.valueOf(d);
                }
            c+=b*Math.pow(16, (a.length()-1-i));//HexaltoDec(a.length()-1-i);
        }else{
            JOptionPane.showMessageDialog(null, "输入的十六进制数有误!");
            return 0 ;
        }
    }
    return c;
}

}

    原文作者:进制转换
    原文地址: https://blog.csdn.net/qq_20215255/article/details/76152879
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞