溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java怎么實現(xiàn)十進制、八進制、十六進制數(shù)互相轉(zhuǎn)換

發(fā)布時間:2020-05-30 02:12:02 來源:億速云 閱讀:851 作者:鴿子 欄目:編程語言

java-十進制、八進制、十六進制數(shù)互相轉(zhuǎn)換
下面是一個十進制、八進制、十六進制數(shù)互相轉(zhuǎn)換的小程序
public class JZTurn {
 
    public static void main(String[] args) {
        int num2 = 1314521;
        System.out.println("十進制數(shù):"+num2);
        System.out.println("十進制轉(zhuǎn)換二進制:"+turn2(num2));
        System.out.println("十進制轉(zhuǎn)換八進制:"+turn8(num2));
        System.out.println("十進制轉(zhuǎn)換十六進制:"+turn16(num2));
         
        //2-> 8
        System.out.println("二進制轉(zhuǎn)換八進制:"+twoTurnEight(turn2(num2)));
        System.out.println("八進制轉(zhuǎn)換二進制:"+eightTurnTwo(turn8(num2)));
        System.out.println("八進制轉(zhuǎn)換十六進制:"+eightTurnSixteen(turn8(num2)));
        //2->16
        System.out.println("二進制轉(zhuǎn)換十六進制:"+twoTurnSixteen(turn2(num2)));
    }
     
    /
      10進制轉(zhuǎn)二進制
     
@param num
      @return
     
/
    public static String turn2(int num){
        String shortTimeNum = "";
        int remainder = 0;
        /

          模擬短除法
         
/
        while(num >= 1){
            remainder = num % 2;
            num = num / 2;
            shortTimeNum = remainder + shortTimeNum;
        }
         
        return shortTimeNum;
    }
     
    /
      10進制轉(zhuǎn)8進制
     
@param num
      @return
     
/
    public static String turn8(int num){
        String shortTimeNum = "";
        int remainder = 0;
        /

          模擬短除法
         
/
        while(num >= 1){
            remainder = num % 8;
            num = num / 8;
            shortTimeNum = remainder + shortTimeNum;
        }
         
        return shortTimeNum;
    }
     
    /
      10進制轉(zhuǎn)16進制
     
@param num
      @return
     
/
    public static String turn16(int num){
        String shortTimeNum = "";
        int remainder = 0;
        String tempStr = "";
        /

          模擬短除法
         
/
        while(num >= 1){
            remainder = num % 16;
            tempStr = getOtherNum(remainder);
             
            num = num / 16;
            shortTimeNum = tempStr + shortTimeNum;
        }
         
        return shortTimeNum;
    }
     
    /
      2進制轉(zhuǎn)換8進制
     
概念說明:這里轉(zhuǎn)換的是整數(shù),從右向左三位一組分別乘以2的零次方,2的一次方,2的2次方
      然后把每組中的數(shù)相加,再把各組從左向右拼接到一起
     
@param strNum2
     /
    public static String twoTurnEight(String strNum2){
        String resultNum = "";
        int remainder = strNum2.length() % 3;
         
        //補位 三位一組,最后一組位數(shù)不夠用0補充
        switch(remainder){
        case 1:
            strNum2 = "00"+strNum2;
            break;
        case 2:
            strNum2 = "0"+strNum2;
            break;
        }
        int index = strNum2.length()-1;
        int tempNum1 = 0;
        int tempNum2 = 0;
        int tempNum3 = 0;
        while(index>=1){
 
            index -- ;
             
            if(index % 3 == 0){
                //每個數(shù)為一組
                tempNum1 = Integer.parseInt(strNum2.substring(index, index+1));
                tempNum2 = Integer.parseInt(strNum2.substring(index+1, index+2));
                tempNum3 = Integer.parseInt(strNum2.substring(index+2, index+3));
                 
                resultNum = tempNum3
1+tempNum22+tempNum14+resultNum;
                 
            }
             
        }
         
        return resultNum;
    }
     
    /

      八進制轉(zhuǎn)換二進制
     
@param eightNum
      @return
     
/
    public static String eightTurnTwo(String eightNum){
        String twoNum = "";
        int index = eightNum.length()-1;
         
        String tempNum = "";
        while(index>=0){
             
            tempNum = turn2(Integer.parseInt(eightNum.substring(index, index+1))) ;
             
            //補位,在轉(zhuǎn)換8進制時是每三為二進制數(shù)為一組,轉(zhuǎn)換回二進制時位數(shù)也需要是三位,不夠用零補
            switch (tempNum.length()) {
             
            case 1:
                tempNum = "00"+tempNum;
                break;
 
            case 2:
                tempNum = "0"+tempNum;
                break;
            }
             
            twoNum = tempNum + twoNum;
            index--;
        }
         
        return twoNum;
    }
     
    /
      2進制轉(zhuǎn)換16進制
     
概念說明:這里轉(zhuǎn)換的是整數(shù),從右向左四位一組分別乘以2的零次方,2的一次方,2的2次方,2的3次方,
      然后相加把每組最終的得數(shù)一次從左向右拼到一起,若其中一組的和大于9,按照對應(yīng)關(guān)系轉(zhuǎn)換后再把每組的結(jié)果拼接到一起
     
@param strNum2
     /
    public static String twoTurnSixteen(String strNum2){
        String resultNum = "";
        int remainder = strNum2.length() % 4;
         
        //補位,四位一組,最后一組位數(shù)不夠用0補充
        switch(remainder){
        case 1:
            strNum2 = "000"+strNum2;
            break;
        case 2:
            strNum2 = "00"+strNum2;
            break;
        case 3:
            strNum2 = "0"+strNum2;
            break;
        }
        int index = strNum2.length()-1;
        int tempNum1 = 0;
        int tempNum2 = 0;
        int tempNum3 = 0;
        int tempNum4 = 0;
        int tempNum5 = 0;
        while(index>=1){
 
            index -- ;
             
            if(index % 4 == 0){
                //每個數(shù)為一組
                tempNum1 = Integer.parseInt(strNum2.substring(index, index+1));
                tempNum2 = Integer.parseInt(strNum2.substring(index+1, index+2));
                tempNum3 = Integer.parseInt(strNum2.substring(index+2, index+3));
                tempNum4 = Integer.parseInt(strNum2.substring(index+3, index+4));
                 
                tempNum5 = tempNum4
1 + tempNum32+tempNum24+tempNum1*8;
                 
                resultNum = getOtherNum(tempNum5) +resultNum;
                 
            }
             
        }
         
        return resultNum;
    }
     
    /*
     
8進制轉(zhuǎn)換16進制
      @param strNum2
     
/
    public static String eightTurnSixteen(String strNum2){
        return twoTurnSixteen(eightTurnTwo(strNum2));
    }
     
    /

      16進制轉(zhuǎn)換中的特殊處理,需要把大于9的數(shù)字轉(zhuǎn)換成字母
     
@param tempNum
      @return
     
/
    public static String getOtherNum(int tempNum){
        String tempStr = "";
        if(tempNum > 9){
            switch(tempNum){
            case 10:
                tempStr = "A";
                break ;
            case 11:
                tempStr = "B";
                break;
            case 12:
                tempStr = "C";
                break;
            case 13:
                tempStr = "D";
                break;
            case 14:
                tempStr = "E";
                break;
            case 15:
                tempStr = "F";
                break;
            }
        }else{
            tempStr = String.valueOf(tempNum);
        }
        return tempStr;
    }
}
輸出結(jié)果:
十進制數(shù):1314521
十進制轉(zhuǎn)換二進制:101000000111011011001
十進制轉(zhuǎn)換八進制:5007331
十進制轉(zhuǎn)換十六進制:140ED9
二進制轉(zhuǎn)換八進制:5007331
八進制轉(zhuǎn)換二進制:101111011011001
八進制轉(zhuǎn)換十六進制:5ED9
二進制轉(zhuǎn)換十六進制:140ED

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI