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

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

How do I convert an array of integers to binary?

问题

  1. for (int i = 0; i < n; i++) {
  2. arr[i] = scanner.nextInt();
  3. }
  4. String[] bin = new String[n];
  5. for (int i = 0; i < n; i++) {
  6. bin[i] = Integer.toBinaryString(arr[i]);
  7. }

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

例如:

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

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

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

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

  1. <details>
  2. <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]);
}

  1. 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.
  2. For Example:
  3. &lt;br/&gt;
  4. If the input array is: 2 3 7 10&lt;br/&gt;
  5. The binary string array will be:
  6. 10&lt;br/&gt;
  7. 11&lt;br/&gt;
  8. 111&lt;br/&gt;
  9. 1010&lt;br/&gt;
  10. But I want the output array to be like the following:&lt;br/&gt;
  11. 0010&lt;br/&gt;
  12. 0011&lt;br/&gt;
  13. 0111&lt;br/&gt;
  14. 1010&lt;br/&gt;
  15. #2&lt;br/&gt;
  16. If the input array is: 2 10 20&lt;br/&gt;
  17. The binary string array will be:&lt;br/&gt;
  18. 10&lt;br/&gt;
  19. 1010&lt;br/&gt;
  20. 10100&lt;br/&gt;
  21. But I want the output array to be like the following:&lt;br/&gt;
  22. 00010&lt;br/&gt;
  23. 01010&lt;br/&gt;
  24. 10100&lt;br/&gt;
  25. </details>
  26. # 答案1
  27. **得分**: 4
  28. 要使所有二进制字符串的长度与最长“字符串”的长度匹配,您可以首先找到最长的长度,然后使用 `String#format` `String#replace` 进行零填充。
  29. ```java
  30. int maxLen = 0;
  31. for (int i = 0; i < n; i++) {
  32. bin[i] = Integer.toBinaryString(arr[i]);
  33. maxLen = Math.max(maxLen, bin[i].length());
  34. }
  35. for (int i = 0; i < n; i++) {
  36. if (bin[i].length() != maxLen)
  37. bin[i] = String.format("%" + maxLen + "s", bin[i]).replace(' ', '0');
  38. System.out.println(bin[i]);
  39. }
英文:

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.

  1. int maxLen = 0;
  2. for (int i = 0; i &lt; n; i++) {
  3. bin[i] = Integer.toBinaryString(arr[i]);
  4. maxLen = Math.max(maxLen, bin[i].length());
  5. }
  6. for (int i = 0; i &lt; n; i++) {
  7. if (bin[i].length() != maxLen)
  8. bin[i] = String.format(&quot;%&quot; + maxLen + &quot;s&quot;, bin[i]).replace(&#39; &#39;, &#39;0&#39;);
  9. System.out.println(bin[i]);
  10. }

答案2

得分: 1

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

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

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.

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

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:

确定