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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

进制转换代码:
  1. class Obj
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 toBin(60);   
  6.                 toOct(60);  
  7.                 toHex(60);
  8.         }
  9.         public static void toBin(int num)
  10.         {
  11.                 trans(num, 1, 1);
  12.         }
  13.         public static void toOct(int num)
  14.         {
  15.                 trans(num, 7, 3);
  16.         }
  17.         public static void toHex(int num)
  18.         {
  19.                 trans(num, 15, 4);
  20.         }
  21.         public static void trans(int num, int base, int offset)
  22.         {       
  23.                 if(num==0)
  24.                 {
  25.                         System.out.println(0);
  26.                         return;
  27.                 }
  28.                 char[] chs={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  29.                 char[] arr=new char[32];
  30.                 int pos=arr.length;
  31.                 while (num!=0)
  32.                 {
  33.                         int temp=num & base;
  34.                         arr[--pos]=chs[temp];
  35.                         num = num >>> offset;
  36.                 }
  37.                 for(int x=pos; x<arr.length; x++)
  38.                 {
  39.                         System.out.print(arr[x]);
  40.                 }
  41.         }
  42. }
复制代码

4 个回复

倒序浏览
学到API就会有更好的方式获取进制数了
回复 使用道具 举报
zhao_HHH 发表于 2015-6-28 17:21
学到API就会有更好的方式获取进制数了

恩 ,自学还在面向对象中。
回复 使用道具 举报
帮顶 吧   没注释  果然看不是很懂
回复 使用道具 举报
学习下,,,,,,个人觉得还是用一些规律性的总结更好让大家接收把.......
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马