- public class day4Test3 {
- public static void main(String[] args) {
- toHex(60);
- trans(60,15,4);
- trans(6,1,1);
- trans(9,7,3);
- }
- public static void toHex(int x){
-
- char[] arr = {'0','1','2','3','4','5','6','7',
- '8','9','a','b','c','d','e','f'};
- char[] ch= new char[8];
- int pos = ch.length;
- while(x!=0){
- int temp = x&15;
- ch[--pos] = arr[temp];
- x=x>>>4;
- }
- System.out.println("pos="+pos);
- for (int y=pos;y<ch.length;y++){
- System.out.print(ch[y]+",");
- }
- }
- public static void trans(int num,int base,int offset){//进制转换通法。
- char[] arr = {'0','1','2','3','4','5','6','7',
- '8','9','a','b','c','d','e','f'};
- char[] ch= new char[8];
- int pos = ch.length;
- while(num!=0){
- int temp = num&base;
- ch[--pos] = arr[temp];
- num=num>>>offset;
- }
- for (int x=pos;x<ch.length;x++){
- System.out.print(ch[x]+",");
- }
- }
- }
复制代码 |
|