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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© woshihuoye 中级黑马   /  2014-12-18 20:46  /  1277 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Study1
{
        public static void main(String[] args)
        {
                jz(6);
        }
        public static void jz(int a)
        {        int temp;
                for(int x=0;x<4;x++)
                {
                        temp = a&15;
                        if (temp>9)
                        {
                                System.out.print((char)(temp-10+'A'));
                        }
               
                        else
                               
                                System.out.print(temp);
                        a = a>>>4;
                }
       
        }
}
如题

13 个回复

倒序浏览
在控制台打印出的是倒序,怎么弄成正序各位大大?
回复 使用道具 举报
直接用API工具不得了吗?
回复 使用道具 举报
直接用API工具不得了吗?
回复 使用道具 举报
lwj123 发表于 2014-12-18 21:42
直接用API工具不得了吗?

练习代码。。。哈哈
回复 使用道具 举报
可以装入数组中,然后逆向打印
回复 使用道具 举报
看看,有没有的大神有更好的思路
回复 使用道具 举报
在下佩服佩服呀。。
回复 使用道具 举报
kenhe 发表于 2014-12-18 22:16
可以装入数组中,然后逆向打印

可以用StringBuffer么?
回复 使用道具 举报
不好意思,我不会
回复 使用道具 举报
Api里面有这种转换的方法
回复 使用道具 举报
wata 中级黑马 2014-12-20 11:43:56
12#
本帖最后由 wata 于 2014-12-20 11:48 编辑
  1. package day04;
  2. /*
  3. * 利用数组+查表法將:
  4. *
  5. * 十进制-->二进制
  6. * 十进制-->八进制
  7. * 十进制-->十六进制
  8. * */
  9. public class toBinToHex2 {
  10.         public static void main(String[] args){
  11.                 toBin(60);
  12.                 System.out.println();
  13.                
  14.                 toBa(60);
  15.                 System.out.println();
  16.                
  17.                 toHex(60);
  18.                 System.out.println();
  19.         }
  20.        
  21.         //进制转换器
  22.         //用法:例如转换八进制,只需把base和offset值分别置为7和3。
  23.         public static void trans(int num, int base, int offset){
  24.                 if(num==0){
  25.                         System.out.print(0);
  26.                         return;
  27.                 }
  28.                 char[] chs = {'0','1','2','3'
  29.                                 ,'4','5','6','7'
  30.                                 ,'8','9','A','B'
  31.                                 ,'C','D','E','F'};
  32.                 char[] arr = new char[32];
  33.                 int pos = arr.length;
  34.                
  35.                 while(num!=0){
  36.                         int temp = num & base;
  37.                         arr[--pos] = chs[temp];
  38.                         num = num >>> offset;
  39.                 }
  40.                
  41.                 for(int x=pos; x<arr.length; x++){
  42.                         System.out.print(arr[x]);
  43.                 }
  44.                
  45.                 return;
  46.         }
  47.        
  48.         //十进制-->二进制
  49.         public static void toBin(int num){
  50.                 trans(num,1,1);
  51.         }
  52.        
  53.         //十进制-->八进制
  54.         public static void toBa(int num){
  55.                 trans(num,7,3);
  56.         }
  57.        
  58.         //十进制-->十六进制
  59.         public static void toHex(int num){
  60.                 trans(num,15,4);
  61.         }
  62. }
复制代码


我觉得这种方法最精简了且最实用了。
回复 使用道具 举报
  1. //直接判断
  2.         private static String toHexBinDecOct(int num,int radix,int offst){
  3.                 String result="";
  4.                 int temp;
  5.                 while(num!=0){
  6.                         temp=num&(radix-1);
  7.                         if(radix>9&&temp>9) result=(char)(temp+'A'-10)+result;
  8.                         else result=temp+result;
  9.                         num=num>>>offst;
  10.                 }
  11.                 return result;
  12.         }
  13.         //采用查表法
  14.         private static String transformDecimal(int num,int radix,int offst){
  15.                 String result="";
  16.                 int temp;
  17.                 char table[]={'0','1','2','3',
  18.                                         '4','5','6','7',
  19.                                         '8','9','A','B',
  20.                                         'C','D','E','F'};
  21.                 while(num!=0){
  22.                         temp=num&(radix-1);
  23.                         result=table[temp]+result;
  24.                         num=num>>>offst;
  25.                 }
  26.                 return result;
  27.         }
复制代码
回复 使用道具 举报
woshihuoye 发表于 2014-12-18 20:48
在控制台打印出的是倒序,怎么弄成正序各位大大?

提供一种思路,递归,然后输出低位
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马