这是的写的进制转换:
- //下面这三个方法分别转换成相应的进制。
- public static void toBin(int num,int x)
- {
- System.out.print("您输入的"+x+"进制数的二进制形式是:");
- trans(num,1,1);
- }
- public static void toOct(int num,int x)
- {
- System.out.print("您输入的"+x+"进制数的八进制形式是:");
- trans(num,7,3);
- }
- public static void toHex(int num,int x)
- {
- System.out.print("您输入的"+x+"进制数的十六进制形式是:");
- trans(num,15,4);
- }
-
-
- //trans():上边三个进制转换函数调用这个方法就可以完成进制的转换。
- public static void trans(int num, int base, int offset)
- {
- if(num == 0)
- {
- System.out.println(0);
- return;
- }
- char[] chs = {'0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F'};
- char[] arr = new char[32];
- int pos = arr.length;
- while(num != 0)
- {
- int temp = num & base;
- arr[--pos] = chs[temp];
- num = num >>> offset;
- }
- for(int i = pos; i < arr.length; i++)
- {
- System.out.print(arr[i]);
- }
- System.out.println();
- System.out.println();
- }
复制代码 我在帖子http://bbs.itheima.com/thread-165988-1-1.html写了我的看法,可惜啊!帖子沉了,写的没深度么?
|