Java – convert binary strings to binary
•
Java
How to convert a string written as binary to binary (byte array)?
If I have a string:
String binary = "0000"
I want the binary data to be 0000
The following is what happens when I set the binary file as a byte array (48 in turn, i.e. ASCII)
Binary string: 0000 binary byte array: 48 binary byte array: 48 binary byte array: 48 binary byte array: 48
I'm not good at explaining, so I hope the above example is enough to tell you what I want
Edit: This is to set the data as a binary file
Solution
Use this:
System.out.println(Integer.toBinaryString(Integer.parseInt("000",2))); // gives 0 System.out.println(Integer.toBinaryString(Integer.parseInt("010",2))); // gives 10 System.out.println(Integer.toBinaryString(Integer.parseInt("100",2))); // gives 100
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码