A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 18255007004 中级黑马   /  2015-1-5 23:25  /  1710 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

进制转换
class Transfer
{
      publicstatic void main(String[] args)
      {
           tobin(6);
           tohex(60);
           toba(60);
      }
        //二进制转换
    publicstatic void tobin(int num)
      {
           trans(num,1,1);
      }
      //十六进制转换
      publicstatic void tohex(int num)
      {
           trans(num,15,4);
      }
     //八进制转换
      publicstatic void toba(int num)
      {
           trans(num,7,3);
      }
   //转换函数
      publicstatic void trans(int num,int base,int offset)
      {
           if(num==0)
           {
                 System.out.println(0);
                 return;
           }
           char[]chs={'0','1','2','3',
                             '4','5','6','7',
                             '8','9','A','B',
                             'C','D','E','F'};
           char[] arr=new char[32];
           intpos=arr.length;
           while(num!=0)
           {
                 inttemp=num&base;
                 arr[--pos]=chs[temp];
                 num=num>>>offset;
           }
           for(int x=pos;x<arr.length ;x++ )
           {
                 System.out.print(arr[x]);
           }
      }
}
体会:java基本数据类型默认值boolean     false       char      null        byte      0      short       0 int            0               long     0L          float    0.0f      double    0.0d
即:java中基本类型的默认值是0,引用类型会默认为null。


评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

11 个回复

正序浏览
谢谢分享,看起来不难,不过做起来我就不给力,还是要认真看一下呗。
回复 使用道具 举报
大赞 ,给力
回复 使用道具 举报
路过,看过。。
回复 使用道具 举报
支持下,顺便学习学习
回复 使用道具 举报
学习了。。。
回复 使用道具 举报
容易被忽略的东西。
回复 使用道具 举报
有点叼!!
回复 使用道具 举报
好吧,支持一下,希望继续努力。。
回复 使用道具 举报
支持下,每天坚持做笔记
回复 使用道具 举报
那么好,支持下
回复 使用道具 举报
感谢分享了,支持
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马