问题描述:通用进制转换方法抽取
代码如下:
public class Tansform {public static void main(String[] args) {trans(100, 4, 15); //转16进制trans(16, 3, 7); //转8进制trans(16, 1, 1); //转2进制}/*** 十进制转换* @param num 要转的数字* @param offset 二进制对应目标进制的位数* @param base offset个二进制数全为1时的值*/public static void trans(int num,int offset,int base){char[] ch = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};char[] result = new char[32];int i = 0;while(num != 0){int temp = num & base;result[i++] = ch[temp];num = num>>>offset;}for (int j = i-1; j >= 0; j--) {System.out.print(result[j]);}System.out.println();}
}