java怎么实现十进制、八进制、十六进制数互相转换-创新互联

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

创新互联建站是一家以网络技术公司,为中小企业提供网站维护、网站建设、成都网站设计、网站备案、服务器租用、域名注册、软件开发、重庆小程序开发等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站咨询电话:028-86922220

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享题目:java怎么实现十进制、八进制、十六进制数互相转换-创新互联
文章源于:https://www.cdcxhl.com/article26/ehjjg.html

成都网站建设公司_创新互联,为您提供网站制作关键词优化营销型网站建设动态网站做网站域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发