查表法进制转换

 1 public class Day043
 2 {
 3     // 查表法进制转换
 4     public static void main(String[] args)
 5     {
 6         System.out.print(getChange(16, 8));
 7     }
 8     // 定义数组
 9     static char[] arr = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8',
10             '9', 'A', 'B', 'C', 'D', 'E', 'F'};
11     // X,需要转化的数据;Y,转化成的进制,包括2进制,8进制,16进制;
12     static String getChange(int x, int y)
13     {
14         StringBuffer sb = new StringBuffer();
15         int temp;
16         int i = 0;
17         int j = y;
18         if (x == 0)
19             return sb.append('0').toString();
20         while (j / 2 != 0)
21         {
22             i++;
23             j = j / 2;
24         }
25         while (x != 0)
26         {
27             temp = x & (y - 1);
28             sb.append(arr[temp]);
29             x = x >>> i;
30         }
31         return sb.reverse().toString();
32     }
33 }

 

 

转载于:https://www.cnblogs.com/linson0116/p/3503105.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值