Java 十进制转二进制
方法一、使用Integer自带方法toBinaryString
int n = 5;
String result = Integer.toBinaryString(n);
方法二、自定义函数
/**
* @author yinglongwu
*/
//十进制转二进制,除以2,倒取余
public static String tenToTwo(int x) {
String s = "";
if (x==0) {
s = "0";
} else {
while (x!=0) {
s = s + Integer.toString(x%2);
x=x/2;
}
}
String result = new StringBuffer(s).reverse().toString();
return result;
}