class BufferDemo
{
public static void main(String[] args)
{
//appendString(6);
//ToHax(60);
//Cha(60);
//toBin(6);
toBa(60);
}
//十进制==>二进制
public static void appendString(int num)
{
StringBuffer sb=new StringBuffer();//建立容器
while(num>0)
{
sb.append(num%2);
num =num/2;
}
System.out.println(sb.reverse());//reverse反转字符串
}
//十进制==>十六进制
public static void ToHax(int num)
{
StringBuffer toH=new StringBuffer();
for(int i=0;i<8;i++)
{
int tem=num & 15;
if(tem>9)
{
toH.append((char)(tem-10+'A'));
}
else
{
toH.append(tem);
}
num=num >>> 4;
}
System.out.println(toH.reverse());
}
//查表法
public static void Cha(int num)
{
char[] arr={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] xin=new char[8];
int pos =xin.length;
while(num !=0)
{
int temp=num&15;
xin[--pos]=arr[temp];
num=num >>> 4;
}
for(int i=pos;i<xin.length;i++)
{
System.out.print(xin[i]);
}
}
//十进制==>二进制
public static void toBin(int num)
{
trans(num,1,1);
}
//十进制==>八进制
public static void toBa(int num)
{
trans(num,7,3);
}
//十进制==>十六进制
public static void toHex(int num)
{
trans(num,15,4);
}
//进制转换优化
public static void trans(int num,int base,int yidong)
{
char[] arr={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] arr_lin=new char[32];
int pos=arr_lin.length;
while(num !=0)
{
int temp=num&base;
arr_lin[--pos]=arr[temp];
num=num >>> yidong;
}
for(int i=pos;i<arr_lin.length;i++)
{
System.out.print(arr_lin[i]);
}
}
} |
|