JAVA中的十进制转换为二进制
发布网友
发布时间:2022-05-14 20:42
我来回答
共4个回答
热心网友
时间:2023-10-29 06:29
public class test {
public static void main(String[] args) {
String m = Integer.toBinaryString(120);
System.out.println(m);
}
}
--------------------------------
public class test {
public static void main(String[] args) {
int m = 13;
String s = "";
while (m > 0) {
s = m % 2 + s;
m = m / 2;
}
System.out.println(s);
}
}
热心网友
时间:2023-10-29 06:29
public class tobin {
public static void main(String[] args) {
int x = 17;//转换前的十进制数
int num=0;
for(int y=x;y!=0;num++)y=y/2;//计算转换后二进制数的位数
int[] bin = new int[num];//转换后的二进制数
for(int i=num-1,y=x;i>=0;i--){
bin[i]=y%2;
y=y/2;
}
for(int i=0;i<num;i++)
System.out.print(bin[i]);
}
}
热心网友
时间:2023-10-29 06:30
public class Bin
{
public static void main(String[] args)
{
StringBuffer sbf = toBin(5);
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;
}
}
热心网友
时间:2023-10-29 06:30
public static void main(String[] args) {
StringBuffer bi = new StringBuffer();
int x = 90;
do{
int y = x%2;
bi.insert(0,y+"");
x = (x - y) / 2;
}while(x > 0);
System.out.println(bi.toString());
}
热心网友
时间:2023-10-29 06:29
public class test {
public static void main(String[] args) {
String m = Integer.toBinaryString(120);
System.out.println(m);
}
}
--------------------------------
public class test {
public static void main(String[] args) {
int m = 13;
String s = "";
while (m > 0) {
s = m % 2 + s;
m = m / 2;
}
System.out.println(s);
}
}
热心网友
时间:2023-10-29 06:29
public class tobin {
public static void main(String[] args) {
int x = 17;//转换前的十进制数
int num=0;
for(int y=x;y!=0;num++)y=y/2;//计算转换后二进制数的位数
int[] bin = new int[num];//转换后的二进制数
for(int i=num-1,y=x;i>=0;i--){
bin[i]=y%2;
y=y/2;
}
for(int i=0;i<num;i++)
System.out.print(bin[i]);
}
}
热心网友
时间:2023-10-29 06:30
public class Bin
{
public static void main(String[] args)
{
StringBuffer sbf = toBin(5);
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;
}
}
热心网友
时间:2023-10-29 06:30
public static void main(String[] args) {
StringBuffer bi = new StringBuffer();
int x = 90;
do{
int y = x%2;
bi.insert(0,y+"");
x = (x - y) / 2;
}while(x > 0);
System.out.println(bi.toString());
}