成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

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

      八進(jìn)制轉(zhuǎn)換二進(jì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))) ;
             
            //補(bǔ)位,在轉(zhuǎn)換8進(jìn)制時(shí)是每三為二進(jìn)制數(shù)為一組,轉(zhuǎn)換回二進(jìn)制時(shí)位數(shù)也需要是三位,不夠用零補(bǔ)
            switch (tempNum.length()) {
             
            case 1:
                tempNum = "00"+tempNum;
                break;
 
            case 2:
                tempNum = "0"+tempNum;
                break;
            }
             
            twoNum = tempNum + twoNum;
            index--;
        }
         
        return twoNum;
    }
     
    /
      2進(jìn)制轉(zhuǎn)換16進(jìn)制
     
概念說明:這里轉(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;
         
        //補(bǔ)位,四位一組,最后一組位數(shù)不夠用0補(bǔ)充
        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){
                //每個(gè)數(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進(jìn)制轉(zhuǎn)換16進(jìn)制
      @param strNum2
     
/
    public static String eightTurnSixteen(String strNum2){
        return twoTurnSixteen(eightTurnTwo(strNum2));
    }
     
    /

      16進(jìn)制轉(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é)果:
十進(jìn)制數(shù):1314521
十進(jìn)制轉(zhuǎn)換二進(jìn)制:101000000111011011001
十進(jìn)制轉(zhuǎn)換八進(jìn)制:5007331
十進(jìn)制轉(zhuǎn)換十六進(jìn)制:140ED9
二進(jìn)制轉(zhuǎn)換八進(jìn)制:5007331
八進(jìn)制轉(zhuǎn)換二進(jìn)制:101111011011001
八進(jìn)制轉(zhuǎn)換十六進(jìn)制:5ED9
二進(jìn)制轉(zhuǎn)換十六進(jìn)制:140ED

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)和平免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

新聞標(biāo)題:java怎么實(shí)現(xiàn)十進(jìn)制、八進(jìn)制、十六進(jìn)制數(shù)互相轉(zhuǎn)換
當(dāng)前地址:http://jinyejixie.com/article20/ppejco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、企業(yè)網(wǎng)站制作小程序開發(fā)、關(guān)鍵詞優(yōu)化、網(wǎng)站維護(hù)服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
维西| 屯门区| 云和县| 土默特左旗| 隆化县| 沛县| 罗山县| 沾化县| 松阳县| 衢州市| 清水河县| 昌江| 扶沟县| 广灵县| 涟源市| 兴安盟| 南充市| 洞口县| 海晏县| 团风县| 渭源县| 许昌市| 苍溪县| 尤溪县| 桂阳县| 宁波市| 来凤县| 米泉市| 铜山县| 兴化市| 荆门市| 蒙自县| 平安县| 山丹县| 巴南区| 延庆县| 娱乐| 龙陵县| 筠连县| 南平市| 铅山县|