如何将整数数组转换为二进制?

huangapple go评论82阅读模式
英文:

How do I convert an array of integers to binary?

问题

for (int i = 0; i < n; i++) {
    arr[i] = scanner.nextInt();
}
String[] bin = new String[n];
for (int i = 0; i < n; i++) {
    bin[i] = Integer.toBinaryString(arr[i]);
}

上述代码将整数数组转换为一个字符串数组(包含输入整数的二进制格式),但有一个注意事项。

例如:

如果输入数组为:2 3 7 10
二进制字符串数组将为:
10
11
111
1010

但我希望输出数组如下所示:
0010
0011
0111
1010

#2
如果输入数组为:2 10 20
二进制字符串数组将为:
10
1010
10100

但我希望输出数组如下所示:
00010
01010
10100


<details>
<summary>英文:</summary>

for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
String[] bin = new String[n];
for (int i = 0; i < n; i++) {
bin[i] = Integer.toBinaryString(arr[i]);
}

The above code will convert the the whole array of integers into an array of `String`s (containing binary format of the input string), but there is a caveat.

For Example:
&lt;br/&gt;
If the input array is: 2  3  7  10&lt;br/&gt;
The binary string array will be:
10&lt;br/&gt;
11&lt;br/&gt;
111&lt;br/&gt;
1010&lt;br/&gt;

But I want the output array to be like the following:&lt;br/&gt;
0010&lt;br/&gt;
0011&lt;br/&gt;
0111&lt;br/&gt;
1010&lt;br/&gt;

#2&lt;br/&gt;
If the input array is: 2  10  20&lt;br/&gt;
The binary string array will be:&lt;br/&gt;
10&lt;br/&gt;
1010&lt;br/&gt;
10100&lt;br/&gt;

But I want the output array to be like the following:&lt;br/&gt;
00010&lt;br/&gt;
01010&lt;br/&gt;
10100&lt;br/&gt;




</details>


# 答案1
**得分**: 4

要使所有二进制字符串的长度与最长“字符串”的长度匹配,您可以首先找到最长的长度,然后使用 `String#format` 和 `String#replace` 进行零填充。

```java
int maxLen = 0;
for (int i = 0; i < n; i++) {
    bin[i] = Integer.toBinaryString(arr[i]);
    maxLen = Math.max(maxLen, bin[i].length());
}
for (int i = 0; i < n; i++) {
    if (bin[i].length() != maxLen)
        bin[i] = String.format("%" + maxLen + "s", bin[i]).replace(' ', '0');
    System.out.println(bin[i]);
}
英文:

To have all of the binary strings match the length of the longest String, you can first find the longest length and then use String#format and String#replace to pad with zeroes.

int maxLen = 0;
for (int i = 0; i &lt; n; i++) {
    bin[i] = Integer.toBinaryString(arr[i]);
    maxLen = Math.max(maxLen, bin[i].length());
}
for (int i = 0; i &lt; n; i++) {
    if (bin[i].length() != maxLen)
        bin[i] = String.format(&quot;%&quot; + maxLen + &quot;s&quot;, bin[i]).replace(&#39; &#39;, &#39;0&#39;);
    System.out.println(bin[i]);
}

答案2

得分: 1

你可以首先计算数组的最大二进制字符串长度,然后使用 Collections.nCopies 在每个二进制字符串表示之前添加所需的额外的 0

int mx = 0;
for (int i = 0; i < n; i++) {
  bin[i] = Integer.toBinaryString(arr[i]);
  mx = Math.max(mx, bin[i].length());
}
for (int i = 0; i < n; i++) {
  bin[i] = String.join("", Collections.nCopies(mx - bin[i].length(), "0")) + bin[i];
}
英文:

You can first calculate the max binary string length of array then use Collections.nCopies to add extra 0 needed before string representation of binary string each.

int mx = 0;
for (int i = 0; i &lt; n; i++) {
  bin[i] = Integer.toBinaryString(arr[i]);
  mx = Math.max(mx, bin[i].length());
}
for (int i = 0; i &lt; n; i++) {
  bin[i] = String.join(&quot;&quot;, Collections.nCopies(mx - bin[i].length(), &quot;0&quot;)) + bin[i];
}

huangapple
  • 本文由 发表于 2020年8月15日 22:31:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63427142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定