进制转换
1 package com.briup.day05;
2 public class ZhuanHuan{
3 public static void trans(int num,int base,int offset){
4 if(num == 0 ) System.out.println(0);
5 char [] chs ={
6 '0','1','2','3',
7 '4','5','6','7',
8 '8','9','A','B',
9 'C','D','E','F'
10 };
11 char [] arr = new char[32];
12 int pos = arr.length;//建立一个类似指针的东西
13 while (num != 0){
14 int temp = num & base;
15 arr[--pos] = chs [temp];
16 num = num >>> offset;
17 }
18 for(int i = pos;i<arr.length;i++){
19 System.out.print(arr[i]);
20 }
21 }
22 //十进制转二进制
23 public static void toBin(int num){
24 trans(num,1,1);
25 }
26 //十进制转八进制
27 public static void toBa(int num){
28 trans(num,7,3);
29 }
30 //十进制转十六进制
31 public static void toHex(int num){
32 trans(num,15,4);
33 }
34 public static void main(String [] args){
35 toBin(6);
36 System.out.println();
37 toBa(6);
38 System.out.println();
39 toHex(60);
40 System.out.println();
41 }
42 }