StriBog Asked:2020-11-27 02:41:27 +0000 UTC2020-11-27 02:41:27 +0000 UTC 2020-11-27 02:41:27 +0000 UTC 字节数组到字符串为“0”和“1” 772 如何将位数组转换为文本,使其看起来像一组 0 和 1? 如果您尝试使用该方法bytes.ToString(),那么会发生这种情况:[B@a0e2ce5我有条件地需要 101100110101 这种转换是否有现成的标准方法? java 3 个回答 Voted MBo 2020-11-27T02:52:39Z2020-11-27T02:52:39Z 旁边有提到的方法和Integer.toBinaryString 请注意,前导零被截断,因此如果需要,请手动填充零。 也toString接受一个论点radix Best Answer user236980 2020-11-27T03:49:08Z2020-11-27T03:49:08Z 选项stream api: Byte[] bytes = {1,0,1,1,0,0,1,1,0,1,0,1}; Arrays.stream(bytes).forEach((b) -> System.out.print(b)); // 101100110101 添加到它method reference: Byte[] bytes = {1,0,1,1,0,0,1,1,0,1,0,1}; Arrays.stream(bytes).forEach(System.out::print); // 101100110101 如果你需要一个数组byte[]: byte[] bytes = {1,0,1,1,0,0,1,1,0,1,0,1}; for (int b : bytes) System.out.print(b); // 101100110101 如果你想装饰: byte[] bytes = {1,0,1,1,0,0,1,1,0,1,0,1}; System.out.println(Arrays.toString(bytes)); // [1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1] Андрей 2020-11-27T03:02:16Z2020-11-27T03:02:16Z 如果您的数组由零和一组成,那么就像这样: System.out.print(Arrays.toString(твой_массив));
旁边有提到的方法和Integer.toBinaryString
请注意,前导零被截断,因此如果需要,请手动填充零。
也
toString接受一个论点radixstream api: 添加到它method reference:byte[]:如果您的数组由零和一组成,那么就像这样:
System.out.print(Arrays.toString(твой_массив));