Java 如何获取存储在字节数组中的字节的二进制值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6393873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the binary values of the bytes stored in byte array
提问by Pramod
i am working on a project that gets the data from the file into a byte array and adds "0" to that byte array until the length of the byte array is 224 bits. I was able to add zero's but i am unable to confirm that how many zero's are sufficient. So i want to print the file data in the byte array in binary format. Can anyone help me?
我正在开发一个项目,该项目将文件中的数据转换为字节数组,并将“0”添加到该字节数组,直到字节数组的长度为 224 位。我能够添加零,但我无法确认有多少个零就足够了。所以我想以二进制格式打印字节数组中的文件数据。谁能帮我?
采纳答案by Bohemian
For each byte:
对于每个字节:
- cast to
int
(happens in the next step via automatic widening ofbyte
toint
) - bitwise-AND with mask 255 to zero all but the last 8 bits
- bitwise-OR with 256 to set the 9th bit to one, making all values exactly 9 bits long
- invoke
Integer.toBinaryString()
to produce a 9-bit String - invoke
String#substring(1)
to "delete" the leading "1", leaving exactly 8 binary characters (with leading zeroes, if any, intact)
- 强制转换为
int
(在下一步中通过自动扩大byte
to 发生int
) - 按位与掩码 255 将除最后 8 位之外的所有位归零
- 按位或与 256 将第 9 位设置为 1,使所有值恰好为 9 位长
- 调用
Integer.toBinaryString()
以生成 9 位字符串 - 调用
String#substring(1)
以“删除”前导“1”,仅留下 8 个二进制字符(如果有前导零,则完整无缺)
Which as code is:
其中作为代码是:
byte[] bytes = "711111111
00000000
11001111
00001001
01100001
01100010
01100011
7\tabc".getBytes();
for (byte b : bytes) {
System.out.println(Integer.toBinaryString(b & 255 | 256).substring(1));
}
Output of above code (always 8-bits wide):
上述代码的输出(总是 8 位宽):
byte[] b = new byte[224];
Arrays.fill(b, 0);
回答by Charlie Martin
Try Integer.toString(bytevalue, 2)
尝试Integer.toString(bytevalue, 2)
Okay, where'd toBinaryString
come from? Might as well use that.
好吧,toBinaryString
从哪里来的?还不如用那个。
回答by Suraj Chandran
First initialize the byte array with 0s:
首先用 0 初始化字节数组:
String string = "10000010";
BigInteger biStr = new BigInteger(string, 2);
System.out.println("binary: " + biStr.toString(2));
System.out.println("hex: " + biStr.toString(16));
System.out.println("dec: " + biStr.toString(10));
Now just fill the array with your data. Any left over bytes will be 0.
现在只需用您的数据填充数组。任何剩余的字节都将为 0。
回答by Josef Panerio
You can work with BigIntegerlike below example, most especially if you have 256 bitor longer.
您可以像下面的示例一样使用BigInteger,尤其是当您有256 位或更长的时候。
Put your array into a string then start from there, see sample below:
将您的数组放入一个字符串中,然后从那里开始,请参阅下面的示例:
String string = "The girl on the red dress.";
byte[] byteString = string.getBytes(Charset.forName("UTF-8"));
System.out.println("[Input String]: " + string);
System.out.println("[Encoded String UTF-8]: " + byteString);
BigInteger biStr = new BigInteger(byteString);
System.out.println("binary: " + biStr.toString(2)); // binary
System.out.println("hex: " + biStr.toString(16)); // hex or base 16
System.out.println("dec: " + biStr.toString(10)); // this is base 10
Another example which accepts bytes:
另一个接受字节的例子:
[Input String]: The girl on the red dress.
[Encoded String UTF-8]: [B@70dea4e
binary: 101010001101000011001010010000001100111011010010111001001101100001000000110111101101110001000000111010001101000011001010010000001110010011001010110010000100000011001000111001001100101011100110111001100101110
hex: 546865206769726c206f6e20746865207265642064726573732e
Result:
结果:
try {
System.out.println("binary to byte: " + biStr.toString(2).getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {e.printStackTrace();}
You can also work to convert Binaryto Byteformat
您还可以将Binary转换为Byte格式
String.format("%256s", biStr.toString(2).replace(' ', '0')); // this is for the 256 bit formatting
Note:For string formatting for your Binary format you can use below sample
注意:对于二进制格式的字符串格式,您可以使用以下示例
##代码##