我想以位字符串形式打印我的字节。我找到了方法
String s = String.format("%8s", Integer.toBinaryString(headerArr[bytenumber])).replace(' ', '0');
System.out.println(s);
但是我得到了 super 奇怪的输出:
11111111111111111111111111111111
00001111
11111111111111111111111111111110
00000111
11111111111111111111111111111100
00000011
11111111111111111111111111111000
00000001
11111111111111111111111111110000
11111111111111111111111111111111
11111111111111111111111111100000
01111111
11111111111111111111111111000000
00111111
11111111111111111111111110000000
00011111
00001111
00001110
00000110
00000100
我怎样才能收到不同的位串表示形式?
最佳答案
Integer.toBinaryString
的文档说明
The unsigned integer value is the argument plus 2^32 if the argument is negative
使用代替
Integer.toBinaryString(headerArr[bytenumber] & 0xff);
String.format("%8s", s)
永远不会缩短字符串 s
。如果 s
少于 8 个字符,它只会在前面添加空格。
关于java - 为什么字节以不同的大小打印为位数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52684812/