码迷,mamicode.com
首页 > 编程语言 > 详细

进制转换 JAVA

时间:2015-02-28 18:22:30      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

1.十进制-->二进制(方法一)

 1 /* 
 2     十进制-->二进制(方法一)
 3 */
 4 class DecToBin1
 5 {
 6     public static void main(String[] args) 
 7     {
 8         toBin(6);
 9     }
10 
11     public static void toBin(int num)
12     {
13         StringBuffer sb = new StringBuffer();        //容器
14         while(num>0)
15         {
16             sb.append(num%2);                        //append:添加
17             num /= 2;
18         }
19         //System.out.println(sb);                    //反输出
20         System.out.println(sb.reverse());            //正输出
21     }
22 }

2.十进制-->二进制(方法二)

1 class DecToBin2
2 {
3     public static void main(String[] args) 
4     {
5         System.out.println(Integer.toBinaryString(6));
6     }
7 }

3.二进制-->十六进制

 1 /*
 2     二进制-->十六进制
 3 */
 4 class BinToHex
 5 {
 6     public static void main(String[] args) 
 7     {
 8         toHex(60);
 9     }
10 
11     public static void toHex(int num)
12     {
13         StringBuffer sb = new StringBuffer();
14         for(int i=0 ; i<8 ; i++)                            //32字节最多分8组
15         {
16             int temp = num & 15;
17             if(temp>9)
18             {
19                 sb.append((char)(temp-10+‘A‘));
20             }
21             else
22             {
23                 sb.append(temp);
24             }
25             num = num>>>4;
26         }
27         sb = sb.reverse();
28         while(sb.charAt(0)==‘0‘)                            //注意 字符0 ‘0‘
29         {
30             sb.deleteCharAt(0);
31         }
32         System.out.println(sb);            
33     }
34 }

/*
查表法:建立一个char数组,将0-F录入,利用角标调用 在建立一个int数组,储存分布结果,最后通过遍历的方法去掉0.
程序略。
*/

 

4.最终优化(十进制转二进制;十进制转八进制;十进制转十六进制)

 1 class Trans
 2 {
 3     public static void main(String[] args) 
 4     {
 5         /*十进制转二进制*/
 6             toBin(60);
 7         /*十进制转八进制*/
 8             toBa(60);
 9         /*十进制转十六进制*/    
10             toHex(60);
11     }
12 
13     public static void toBin(int num)
14     {
15         trans(num,1,1);
16     }
17     
18     public static void toBa(int num)
19     {
20         trans(num,7,3);
21     }
22     
23     public static void toHex(int num)
24     {
25         trans(num,15,4);
26     }
27     
28     public static void trans(int num,int base,int offset)
29     {
30         char[] chs = {‘0‘,‘1‘,‘2‘,‘3‘,
31                       ‘4‘,‘5‘,‘6‘,‘7‘,
32                       ‘8‘,‘9‘,‘A‘,‘B‘,
33                       ‘C‘,‘D‘,‘E‘,‘F‘};
34         char[] arr = new char[32];
35         int pos = arr.length;
36         while(num!=0)
37         {
38             int temp = num & base;
39             arr[--pos] = chs[temp];
40             num = num >>> offset;
41         }
42         for(int i=pos ; i<arr.length ; i++)
43         {
44             System.out.print(arr[i]);
45         }
46         System.out.println();
47     }
48 }

 

进制转换 JAVA

标签:

原文地址:http://www.cnblogs.com/yangshenghui/p/4305710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!