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;
}