中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

java怎么實現十進制、八進制、十六進制數互相轉換

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

java-十進制、八進制、十六進制數互相轉換
下面是一個十進制、八進制、十六進制數互相轉換的小程序
public class JZTurn {
 
    public static void main(String[] args) {
        int num2 = 1314521;
        System.out.println("十進制數:"+num2);
        System.out.println("十進制轉換二進制:"+turn2(num2));
        System.out.println("十進制轉換八進制:"+turn8(num2));
        System.out.println("十進制轉換十六進制:"+turn16(num2));
         
        //2-> 8
        System.out.println("二進制轉換八進制:"+twoTurnEight(turn2(num2)));
        System.out.println("八進制轉換二進制:"+eightTurnTwo(turn8(num2)));
        System.out.println("八進制轉換十六進制:"+eightTurnSixteen(turn8(num2)));
        //2->16
        System.out.println("二進制轉換十六進制:"+twoTurnSixteen(turn2(num2)));
    }
     
    /
      10進制轉二進制
     
@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進制轉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進制轉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進制轉換8進制
     
概念說明:這里轉換的是整數,從右向左三位一組分別乘以2的零次方,2的一次方,2的2次方
      然后把每組中的數相加,再把各組從左向右拼接到一起
     
@param strNum2
     /
    public static String twoTurnEight(String strNum2){
        String resultNum = "";
        int remainder = strNum2.length() % 3;
         
        //補位 三位一組,最后一組位數不夠用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){
                //每個數為一組
                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;
    }
     
    /

      八進制轉換二進制
     
@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))) ;
             
            //補位,在轉換8進制時是每三為二進制數為一組,轉換回二進制時位數也需要是三位,不夠用零補
            switch (tempNum.length()) {
             
            case 1:
                tempNum = "00"+tempNum;
                break;
 
            case 2:
                tempNum = "0"+tempNum;
                break;
            }
             
            twoNum = tempNum + twoNum;
            index--;
        }
         
        return twoNum;
    }
     
    /
      2進制轉換16進制
     
概念說明:這里轉換的是整數,從右向左四位一組分別乘以2的零次方,2的一次方,2的2次方,2的3次方,
      然后相加把每組最終的得數一次從左向右拼到一起,若其中一組的和大于9,按照對應關系轉換后再把每組的結果拼接到一起
     
@param strNum2
     /
    public static String twoTurnSixteen(String strNum2){
        String resultNum = "";
        int remainder = strNum2.length() % 4;
         
        //補位,四位一組,最后一組位數不夠用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){
                //每個數為一組
                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進制轉換16進制
      @param strNum2
     
/
    public static String eightTurnSixteen(String strNum2){
        return twoTurnSixteen(eightTurnTwo(strNum2));
    }
     
    /

      16進制轉換中的特殊處理,需要把大于9的數字轉換成字母
     
@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;
    }
}
輸出結果:
十進制數:1314521
十進制轉換二進制:101000000111011011001
十進制轉換八進制:5007331
十進制轉換十六進制:140ED9
二進制轉換八進制:5007331
八進制轉換二進制:101111011011001
八進制轉換十六進制:5ED9
二進制轉換十六進制:140ED

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

调兵山市| 西吉县| 金堂县| 微博| 宜宾县| 横峰县| 高清| 霍林郭勒市| 雷山县| 和顺县| 新密市| 伊宁市| 东光县| 沭阳县| 清流县| 兰考县| 嘉祥县| 漳州市| 和龙市| 敦化市| 郓城县| 繁昌县| 五原县| 邢台县| 黄大仙区| 荔波县| 丽水市| 绥中县| 盐池县| 莒南县| 正宁县| 武陟县| 太仆寺旗| 海口市| 黑山县| 渝中区| 大田县| 九龙县| 伊春市| 拜泉县| 奉新县|