Java 十进制转十六进制(右移符号的完美运用)

1). 源码

 1 //十进制数转十六进制
 2 import java.util.Scanner;
 3 
 4 public class Test01{
 5     public static void main(String[] args){
 6         while(true)
 7         {
 8             System.out.print("Please input an integer:");
 9 
10             Scanner s = new Scanner(System.in);
11             int n = s.nextInt();
12            
13             DataConvert DC = new DataConvert();
14             DC.Decimal_To_Hexadecimal(n);
15         }
16 
17     }
18 }
19 
20 //使用右移运算符">>"
21 class DataConvert{
22     public void Decimal_To_Hexadecimal(long n){
23         String[] k = new String[8];
24         int icount = 0;
25 
26         while(icount <= 7){
27             k[icount++] = ((n&15) > 9)?(char)((n&15) - 10 + 'a') + "":(n&15) + "";
28             n = n>>4;
29         }
30 
31         icount -= 1;
32 
33         while(icount >= 0){
34             System.out.print(k[icount--]);
35         }
36         System.out.println("");
37     }
38 };
View Code

2). 分析:用100做实验(即:01100100)

总结:100的十六进制是“64”

 

posted @ 2016-01-06 16:52  njuptlwh  阅读(936)  评论(0编辑  收藏  举报