用java語言編寫進制轉換

用java語言編寫進制轉換

**

public class jinzhi2 {
public static void main(String[] args) {
    trans(6, 1, 1);//把十進制的6轉換爲二進制
    trans(60,15,4);//把使勁值得60轉換爲十六進制
}
public static void trans(int num ,int base ,int offset){//num:要轉換的數,base:與上的數//offset:偏移位數
    //建表
    char [] chs = {'0','1','2','3',
               '4','5','6','7',
               '8','9','A','B',
               'C','D','E','F'};
    char [] arr = new char [32];//定義一個容器
    int pos = arr.length;
    while(num!=0){
        int temp = num & base;

        arr[--pos]= chs[temp];

        num = num >>>offset;
    }
    for (int i = pos; i < arr.length; i++) {
        System.out.print(arr[i]);
    }
    }

**

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章