4

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.

asked Jul 21, 2014 at 23:07
10
  • possible duplicate of Convert Java String to byte[] array Commented Jul 21, 2014 at 23:10
  • At least @Braj knows what Im talking about. PM 77-1 This is in no possible way a duplicate of Coverting a Java String to a byte[] array. Im aware of how to do that, Im NOT aware however, of how to put the actual input string value and set it as binary. Commented Jul 21, 2014 at 23:12
  • OK I got it your point. Let me update it. Commented Jul 21, 2014 at 23:14
  • @LinkTheProgrammer - You seem to be confused about different represenations of a value. Commented Jul 21, 2014 at 23:19
  • You have to mention that you want decimal value from binary. Commented Jul 21, 2014 at 23:23

4 Answers 4

4

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
answered Jul 21, 2014 at 23:15
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I was looking for! And well, probably the only place too that has this code...
So is this any different than just stripping leading 0's off the input string, without doing any binary/decimal conversions? (Assuming the input is valid.)
Well not having to strip anything, this is more efficient. Plus Strings are NOT my forte.
1

Maybe you want this:

int i = Integer.valueOf(binary, 2); // ie base 2

This call expects the input to be a string of 0 and 1 chars.

Then if you want an array of bytes:

byte[] bytes = new ByteBuffer().putInt(i).compact().array();
answered Jul 21, 2014 at 23:16

13 Comments

This will give 4 for the input "100".
I want the binary data to be 0000. what does it mean. The value is in decimal and OP wants to convert it into binary.
@AlexandreSantos yes - I think hhat's what OP wants, but the question is a bit unclear - you could well be right that OP wants a different result.
@Braj that's not how I read the question: there are only 0 chars in his example input, so we can't be sure.
yes that's why I don't want to delete my post to make it helpful for others.
|
1

Convert into Decimal from Binary

System.out.println(new BigInteger("1010",2).toString()); // 10 decimal

Convert into Binary/Octal/Hex from Decimal

You can use BigInteger#toString(radix) method to get value in any radix.

System.out.println(new BigInteger("10").toString(2)); // 1010 binary
System.out.println(new BigInteger("10").toString(8)); // 12 octal
System.out.println(new BigInteger("10").toString(16)); // a hexadecimal

Let me explain you a bit more how it works with different base

(10)10 = (1010)2

(10)10 = (12)8

(10)10 = (a)16

answered Jul 21, 2014 at 23:12

4 Comments

ehm I already tried that, I dont want ASCII, I want the String to be the actual binary data in my file, otherwise I end up with 48 48 48 48 WHICH ISNT BINARY.
This is no doubt helpful, System.out.println(newBigInteger(int radix).toString(2)); however this is a String input that I want to write as text and then put into a new binary file. Not something confusing for the consumer of the software I want to make.
have a look at updated post with base value. It's simple mathematics.
Yeah, but I mean the highest value you can get with a 4-bit binary value is 256. Im talking ALLLLLL binary here. Nothing really to do with decimals at all. This is binary in String form that I want to write to a binary file. And dang it is hard for me to explain :(
1

JBBP framework has in utils a special method to convert a string contains a binary defined data into byte array

 byte [] converted = JBBPUtils.str2bin("00000000");
answered Sep 6, 2014 at 10:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.