package com.java.learn;
import java.util.Scanner;
import static java.lang.Integer.toBinaryString;
public class reverse {
public static void main (String []args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个整数:");
int num =scanner.nextInt();
StringBuilder sb=new StringBuilder();
while (true){
if(num==0){
break;
}
int remaindar=num%2; //余数
sb.insert(0,remaindar);
num=num/2;
}
System.out.println("对应二进制为:"+sb.toString());
//可直接调用pbulic static String toBinaryString(int i)方法实现
// toBinaryString(int i)十进制转换为八进制
// toBinaryString(int i)十进制转换为十六进制
int num2=3;
String str=toBinaryString(num2);
System.out.println("2222对应二进制为:"+str);
}
}
十进制转换为二进制
于 2023-03-26 21:49:17 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)