例如: 5的二进制转换方法为 5除以2 得余数1 然后再用商值2继续除以2 得余数0 然后除尽了商值为1 所以 最后得到二进制值为 101
public static void main(String[] args)
{
StringBuffer sbf = toBin(10);
String str=sbf.reverse().toString();
System.out.println(str);
}
static StringBuffer toBin(int x)
{
StringBuffer result=new StringBuffer();
do{
result.append(x%2);
x/=2;
}while(x>0);
return result;
}
时间: 2024-12-11 06:15:38