十进制和二进制之间的相互转化
class Demo2
{
public static void main(String[] args)
{
toBinary(6); //调用了递归函数
toBin(48);
}
//十进制转换二进制
public static void toBinary(int num)
{
if(num == 0)
return ;
toBinary(num/2);
System.out.println(num%2);
}
public static void toBin(int num)
{
StringBuffer sb = new StringBuffer();
while(num > 0)
{
sb.append(num % 2);
num /= 2;
}
System.out.print(sb.reverse());
}
}
十进制和八进制的转化
class Demo2
{
public static void main(String[] args)
{
toHex(60);
System.out.println();
toHex1(60);
}
//通过查表法来进行进制转换,temp的值作为数组的索引
public static void toHex1(int num)
{
StringBuffer sb = new StringBuffer();
char[] str= {'0','1','2','3','4','5','6','7','8','9','A','B','C