问题

我有一个数字,我想用二进制打印它。我不想通过编写算法来实现它,在Java中是否有任何内置函数?


#1 热门回答(357 赞)

假设你的意思是"内置":

int x = 100;
System.out.println(Integer.toBinaryString(x));

SeeInteger documentation

(Long有一个类似的方法,BigInteger有一个实例方法,你可以在其中指定基数。)


#2 热门回答(219 赞)

这里不需要只依赖于二进制或任何其他格式...一个灵活的内置函数可用于打印你想要的程序格式.. Integer.toString(int,representation);

Integer.toString(100,8) // prints 144 --octal representation

Integer.toString(100,2) // prints 1100100 --binary representation

Integer.toString(100,16) //prints 64 --Hex representation

#3 热门回答(43 赞)

System.out.println(Integer.toBinaryString(343));

原文链接