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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 佐佑 中级黑马   /  2015-3-27 21:48  /  398 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用位移的问题解决进制的问题,谁有比较完整的代码

1 个回复

倒序浏览
  1. /*
  2. 0 1 2 3 4 5 6 7 8 9 a  b  c  d  e  f 十六进制
  3. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  4. 查表法:将所有的元素临时存储起来,建立对应关系,
  5. 每一次 & 15 后的值做为所引去查建立好的表,就可以找到对应的元素
  6. 这样比-10+ ‘a’简单的多

  7. */

  8. class ArrayTest6
  9. {
  10.         public static void main(String[] args)
  11.         {
  12.                 //toHex(60);
  13.                 //toBin(-6);
  14.                 toBa(8);
  15.                 //System.out.println("Hello World!");
  16.         }
  17.         //优化后的函数调用
  18.         /*
  19.         十进制-->二进制
  20.         public static void toBin(int num)
  21.         {
  22.                 trans(num,1,1);
  23.         }

  24.         */

  25.         //十进制转化成八进制
  26.         public static void toBa(int num)
  27.         {
  28.                 trans(num,7,3);
  29.         }
  30.        

  31.         /*
  32.         十进制转化成十六进制
  33.         public static void toHex(int num)
  34.         {
  35.                 trans(num,15,4);
  36.         }
  37.         */
  38.        
  39.         //查表法10 -->16进制
  40.         public static void toHex(int num)
  41.         {
  42.                 char [] chs={'0','1','2','3',
  43.                                         '4','5','6','7',
  44.                                         '8','9','A','B',
  45.                                         'C','D','E','F',};
  46.                 char [] arr=new char[8];
  47.                 int pos = arr.length -1;
  48.                 while(num!=0)
  49.                 {
  50.                         int temp = num & 15;
  51.                         //System.out.print(chs[temp]);
  52.                         arr[pos--] =chs[temp];
  53.                         num = num >>> 4;
  54.                 }
  55.                 for (int i =pos+1;i<arr.length ;i++ )
  56.                 {
  57.                         System.out.print(arr[i]);
  58.                 }
  59.         }
  60.         //查表法 10-->2进制
  61.         public static void toBin(int num)
  62.         {
  63.                 //定义二进制表
  64.                 char [] chs = {'0','1'};
  65.                 char [] arr = new char[32];
  66.                 int pos = arr.length;
  67.                 while (num!=0)
  68.                 {
  69.                         int temp = num & 1;
  70.                         arr[--pos] = chs[temp];
  71.                         num = num >>>1;
  72.                 }
  73.                 for (int i =pos;i <arr.length  ;i++ )
  74.                 {
  75.                         System.out.print(arr[i]);
  76.                 }
  77.                
  78.         }
  79.         //进制转化中,有相同的语句,整理优化。
  80.         public static void trans(int num,int x,int y)
  81.         {
  82.                 //定义一个通表
  83.                 char [] chs  = {'0','1','2','3',
  84.                                                 '4','5','6','7',
  85.                                                 '8','9','a','b',
  86.                                                 'c','d','e','f',};
  87.                 char [] arr=new char [32];
  88.                 int pos = arr.length;
  89.                 while (num!=0)
  90.                 {
  91.                         int temp = num &x;
  92.                         arr[--pos] = chs[temp];
  93.                         num = num >>> y;
  94.                 }
  95.                 for (int i = pos;i <arr.length ;i++ )
  96.                 {
  97.                         System.out.print(arr[i]);
  98.                 }
  99.         }

  100. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马