1. Overview
1.概述
In this tutorial, we’ll look briefly at the different ways of printing an integer in binary format in Java.
在本教程中,我们将简要介绍在Java中以二进制格式打印整数的不同方法。
First, we’ll take a conceptual look. And then, we’ll learn some built-in Java functions for conversion.
首先,我们将从概念上看一下。然后,我们将学习一些用于转换的内置Java函数。
2. Using Integer to Binary Conversion
2.使用整数到二进制的转换
In this section, we’ll write our custom method to convert an integer into a binary format string in Java. Before writing the code, let’s first understand how to convert an integer into a binary format.
在本节中,我们将写出我们的自定义方法,在Java中把一个整数转换成二进制格式的字符串。在写代码之前,让我们先了解一下如何将一个整数转换成二进制格式。
To convert an integer n into its binary format, we need to:
要将一个整数n转换成其二进制格式,我们需要。
- Store the remainder when number n is divided by 2 and update the number n with the value of the quotient
- Repeat step 1 until the number n is greater than zero
- Finally, print the remainders in reverse order
Let’s see an example of converting 7 into its binary format equivalent:
让我们看一个将7转换为二进制格式等价物的例子。
- First, divide 7 by 2: remainder 1, quotient 3
- Second, divide 3 by 2: remainder 1, quotient 1
- Then, divide 1 by 2: remainder 1, quotient 0
- And finally, print the remainders in reverse order since the quotient in the previous step is 0: 111
Next, let’s implement the above algorithm:
接下来,我们来实现上述算法。
public static String convertIntegerToBinary(int n) {
if (n == 0) {
return "0";
}
StringBuilder binaryNumber = new StringBuilder();
while (n > 0) {
int remainder = n % 2;
binaryNumber.append(remainder);
n /= 2;
}
binaryNumber = binaryNumber.reverse();
return binaryNumber.toString();
}
3. Using Integer#toBinaryString Method
3.使用Integer#toBinaryString方法
Java’s Integer class has a method named toBinaryString to convert an integer into its binary equivalent string.
Java的Integer类有一个名为toBinaryString的方法,将一个整数转换成其二进制等价字符串。
Let’s look at the signature of the Integer#toBinaryString method:
我们来看看Integer#toBinaryString方法的签名。
public static String toBinaryString(int i)
It takes an integer argument and returns a binary string representation of that integer:
它接受一个整数参数并返回该整数的二进制字符串表示。
int n = 7;
String binaryString = Integer.toBinaryString(n);
assertEquals("111", binaryString);
4. Using Integer#toString Method
4.使用Integer#toString方法
Now, let’s look at the signature of the Integer#toString method:
public static String toString(int i, int radix)
The Integer#toString method is an in-built method in Java that takes two arguments. First, it takes an integer that is to be converted to a string. Second, it takes radix that is to be used while converting the integer into its string representation.
Integer#toString方法是Java中内置的方法,需要两个参数。首先,它接收一个要转换为字符串的整数。其次,它需要在将整数转换为字符串表示时使用的弧度。
It returns a string representation of the integer input in the base specified by the radix.
它返回整数输入的字符串表示,其基数由弧度指定。
Let’s use this method to convert an integer into its binary format using a radix value of 2:
让我们用这个方法将一个整数转换成它的二进制格式,使用2的弧度值。
int n = 7;
String binaryString = Integer.toString(n, 2);
assertEquals("111", binaryString);
As we can see that we passed the radix value of 2 while calling the Integer#toString method to convert the integer n into its binary string representation.
我们可以看到,在调用Integer#toString方法将整数n转换为二进制字符串表示时,我们传递了2的拉德值。
5. Conclusion
5.总结
In conclusion, we looked at integer to binary conversion. Furthermore, we saw a couple of built-in Java methods to convert an integer into a string in binary format.
最后,我们研究了整数到二进制的转换。此外,我们还看到了几个内置的Java方法,将一个整数转换成二进制格式的字符串。
As always, all these code samples are available over on GitHub.
一如既往,所有这些代码样本都可以在GitHub上找到。