- /*
- 利用查表法实现进制转换
- 思路:
- 1,定义16进制的表;
- 2,&15查表,StringBuffer添加查表结果;
- 3,反向打印StringBuffer.reverse()。
- */
- class ArrayTest{
- public static void main(String[] args){
- //60的16进制转换
- toTrans(60,16,4);
- }
-
- public static void toTrans(int num, int base, int offset){
- char[] chs = {'0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F'};
- StringBuffer sb = new StringBuffer();
-
- while(num!=0){
- int temp = num&(base-1);
- sb.append(chs[temp]);
- //16进制每4位表示一位,即右移4位
- num = num >>> offset;
- }
- System.out.println(sb.reverse());
- }
- }
复制代码
|
|