亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

崇左市| 新丰县| 乳山市| 苏尼特左旗| 巢湖市| 望奎县| 巴林左旗| 武清区| 四会市| 大石桥市| 蕲春县| 皮山县| 鄂伦春自治旗| 启东市| 荔波县| 洞头县| 朝阳区| 凌海市| 从化市| 大悟县| 清镇市| 若尔盖县| 罗甸县| 达州市| 青岛市| 秭归县| 丹凤县| 临漳县| 林周县| 河池市| 溧阳市| 新竹市| 容城县| 广水市| 怀来县| 肇州县| 金秀| 娄底市| 中阳县| 中江县| 岳普湖县|