十进制数字转换为二进制:
public static void main(String[] args)
{
int n=18;
int i=0;
int[] a=new int[100];
while(n!=0)
{
a[++i]=n%2;
n/=2;
}
System.out.println("转换为二进制为:");
while(i>0)
{
System.out.print(a[i]);
i--;
}
}
十进制数字转换为十六进制:
public static void main(String[] args)
{
int n=18;
int i=0;
int[] a=new int[100];
char[] b={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(n!=0)
{
a[++i]=n%16;
n/=16;
}
System.out.println("转换为16进制为:");
while(i>0)
{
System.out.print(b[a[i]]);
i--;
}
}
转载:https://blog.csdn.net/wyhzrp/article/details/102485548
查看评论