本帖最后由 翁发达 于 2012-7-19 08:44 编辑
我边看视频,学着自已写了一函数,用来作进制转换。
可是结果60转16进制老是显示C3C,我怎么检查也没查出是哪里的问题,我都眼花缭乱了,请哪位朋友帮忙指出错误,谢谢
class ZhuanHuan
{
public static void main(String[] args)
{
toBin(60);
}
public static void toBin(int x)
{
zhuanHuan(x,15,4);
}
public static void zhuanHuan(int x,int y,int z)
{
if (x==0)
{
System.out.println(0);
return;
}
char[] arr={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] arr1=new char[32];
int pos =arr1.length;
while (x!=0)
{
if (y==1)
{
int a = x&y;
arr1[--pos]=arr[a];
x= x >>>z;
}
if (y==7)
{
int a = x&y;
x=x>>>z;
arr1[--pos]=arr[a];
}
if (y==15)
{
int a = x&y;
arr1[--pos]=arr[a];
x=x>>>z;
}
for (int b=pos;b<arr1.length ;b++ )
{
System.out.print(arr1);
}
}
}
|