我有一个数字,我想把它打印成二进制。我不想通过写算法来实现。
Java中有没有内置函数?
我有一个数字,我想把它打印成二进制。我不想通过写算法来实现。
Java中有没有内置函数?
当前回答
我需要一些东西可以很好地打印出来,并把每n位的比特分开。换句话说,显示前导零并显示如下内容:
n = 5463
output = 0000 0000 0000 0000 0001 0101 0101 0111
我是这么写的:
/**
* Converts an integer to a 32-bit binary string
* @param number
* The number to convert
* @param groupSize
* The number of bits in a group
* @return
* The 32-bit long bit string
*/
public static String intToString(int number, int groupSize) {
StringBuilder result = new StringBuilder();
for(int i = 31; i >= 0 ; i--) {
int mask = 1 << i;
result.append((number & mask) != 0 ? "1" : "0");
if (i % groupSize == 0)
result.append(" ");
}
result.replace(result.length() - 1, result.length(), "");
return result.toString();
}
像这样调用它:
public static void main(String[] args) {
System.out.println(intToString(5463, 4));
}
其他回答
我需要一些东西可以很好地打印出来,并把每n位的比特分开。换句话说,显示前导零并显示如下内容:
n = 5463
output = 0000 0000 0000 0000 0001 0101 0101 0111
我是这么写的:
/**
* Converts an integer to a 32-bit binary string
* @param number
* The number to convert
* @param groupSize
* The number of bits in a group
* @return
* The 32-bit long bit string
*/
public static String intToString(int number, int groupSize) {
StringBuilder result = new StringBuilder();
for(int i = 31; i >= 0 ; i--) {
int mask = 1 << i;
result.append((number & mask) != 0 ? "1" : "0");
if (i % groupSize == 0)
result.append(" ");
}
result.replace(result.length() - 1, result.length(), "");
return result.toString();
}
像这样调用它:
public static void main(String[] args) {
System.out.println(intToString(5463, 4));
}
tostring (value,numbersystem)——使用的语法 然后传递值
Integer.toString(100,8) //打印144—八进制
Integer.toString(100,2) //打印1100100—二进制
Integer.toString(100,16) //打印64—十六进制
输入任何十进制数作为输入。在此之后,我们进行模运算和除法运算,将给定的输入转换为二进制数。 这是Java程序的源代码,将整数值转换为二进制和他的十进制数的二进制位数。 Java程序编译成功并在Windows系统上运行。程序输出如下所示。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int integer ;
String binary = ""; // here we count "" or null
// just String binary = null;
System.out.print("Enter the binary Number: ");
integer = sc.nextInt();
while(integer>0)
{
int x = integer % 2;
binary = x + binary;
integer = integer / 2;
}
System.out.println("Your binary number is : "+binary);
System.out.println("your binary length : " + binary.length());
}
}
简单又简单的解决方法。
public static String intToBinaryString(int integer, int numberOfBits) {
if (numberOfBits > 0) { // To prevent FormatFlagsConversionMismatchException.
String nBits = String.format("%" + numberOfBits + "s", // Int to bits conversion
Integer.toBinaryString(integer))
.replaceAll(" ","0");
return nBits; // returning the Bits for the given int.
}
return null; // if the numberOfBits is not greater than 0, returning null.
}
解决方案使用32位显示掩码,
public static String toBinaryString(int n){
StringBuilder res=new StringBuilder();
//res= Integer.toBinaryString(n); or
int displayMask=1<<31;
for (int i=1;i<=32;i++){
res.append((n & displayMask)==0?'0':'1');
n=n<<1;
if (i%8==0) res.append(' ');
}
return res.toString();
}
System.out.println(BitUtil.toBinaryString(30));
O/P:
00000000 00000000 00000000 00011110