Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Binary String to Binary

How do I convert a String written as Binary, to binary (in byte array)?

If I have a String:

String binary = "0000"

I want the binary data to be 0000.

below is what happens when I set the binary to a byte array (which in turn returns 48, which is 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 hopefully the above example was enough to tell you what I want.

EDIT: This is to set the data into a binary file.

like image 475
AMDG Avatar asked Mar 18 '23 23:03

AMDG


1 Answers

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
like image 67
Alexandre Santos Avatar answered Mar 24 '23 07:03

Alexandre Santos