资源简介
Java把十进制转成二进制(任意进制)的程序
代码片段和文件信息
public class DecimalToBinary{
public
String DtoB( int de )
{
// binary number
String bin = ““;
// decimal number
int mydata = de;
// binary
int systemvalue = 2;
while (mydata != 0)
{
// the last bit number
int output = mydata % systemvalue;
String thisStr = Integer.toString(output);
bin = thisStr + bin;
mydata = mydata - output;
mydata = mydata/systemvalue;
}
return bin;
}
public static void main(String[] args){
DecimalToBinary DTB = new DecimalToBinary();
int ori = 16000;
String myBinStr = DTB.DtoB( ori );
String sysBinStr = Integer.toBinaryString(ori);
System.out.println(“Using my own method convert decimal 16000 to binary: “ + myBinStr);
System.out.println(“Using system method convert decimal 16000 to binary: “ + sysBinStr);
System.out.println(“table for binary numbers from 1 to 128 decimal“);
for (int i = 1; i <= 128; i++)
{
System.out.println(Integer.toString(i) + “ “ + DTB.DtoB( i ));
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1066 2010-11-08 00:09 src\DecimalToBinary.java
文件 1879 2010-11-08 00:12 src\output.txt
目录 0 2010-11-08 00:12 src
----------- --------- ---------- ----- ----
2945 3
评论
共有 条评论