本帖最后由 U芽Lady 于 2013-4-10 21:36 编辑
//十进制转换成二进制,八进制,十六进制
class Shi {
public static void main(String[] args) {
fun1(60,15,4);
String s = fun2(60,16);
System.out.print(s);
}
public static void fun1(int num,int j,int k){
char[] c1 = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};//查表
//定义字符串接收转换后的值
char[] c2 = new char[32];
//定义一个指针
int pos = c2.length-1;
//遍历
while(num != 0){
int x = num & j;//与上个进制中的最大值
c2[pos] = c1[x];//把这个地址对应的值付给c2
num = num >>> k;//控制循环
pos--;//指针移动
}
//遍历输出
for (int y = pos;y < c2.length ;y++ ){
System.out.print(c2[y] + " ");
}
}
//这是我自己想的
public static String fun2(int i,int j){
char[] c1 = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};//查表
String s = "";//定义一个字符串接收计算值
for (;i != 0; i = i / j){
int x = i % j;//进制取余
s = c1[x] + s;//返回值
}
return s;
}
}
各位大神帮忙分析一下,在此谢过,我感觉我写的么啥问题,还很简单易懂
|