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.

AMDG
  • 1,118
  • 1
  • 13
  • 29
  • possible duplicate of [Convert Java String to byte\[\] array](http://stackoverflow.com/questions/18571223/convert-java-string-to-byte-array) – PM 77-1 Jul 21 '14 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. – AMDG Jul 21 '14 at 23:12
  • OK I got it your point. Let me update it. – Braj Jul 21 '14 at 23:14
  • @LinkTheProgrammer - You seem to be confused about different *represenations* of a value. – PM 77-1 Jul 21 '14 at 23:19
  • You have to mention that you want **decimal value from binary.** – Braj Jul 21 '14 at 23:23
  • I was deducted a point because in what way is this NOT helpful!? This isn't the same thing as `byte[] b = str.getBytes();`!!! – AMDG Jul 21 '14 at 23:23
  • @Braj but its the other way around. I want decimal value TO binary... allow me to edit the post... – AMDG Jul 21 '14 at 23:24
  • I was confused with `0000` in what format it is 1. decimal 2. binary? – Braj Jul 21 '14 at 23:25
  • have a look at my post to make it vice-versa. – Braj Jul 21 '14 at 23:32
  • If you are dealing with Java strings, 48 is not ASCII, it's a Unicode/UTF-16 code-unit. In this case, the single code-unit needed for the U+0030 codepoint ([DIGIT ZERO](http://www.fileformat.info/info/unicode/char/30/index.htm)). – Tom Blodget Jul 22 '14 at 00:00

4 Answers4

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
Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
  • This is exactly what I was looking for! And well, probably the only place too that has this code... – AMDG Jul 21 '14 at 23:21
  • 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.) – ajb Jul 21 '14 at 23:37
  • Well not having to strip anything, this is more efficient. Plus Strings are NOT my forte. – AMDG Jul 21 '14 at 23:38
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

Braj
  • 46,415
  • 5
  • 60
  • 76
  • 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. – AMDG Jul 21 '14 at 23:13
  • 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. – AMDG Jul 21 '14 at 23:35
  • have a look at updated post with base value. It's simple mathematics. – Braj Jul 21 '14 at 23:36
  • 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 :( – AMDG Jul 21 '14 at 23:40
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();
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    This will give 4 for the input "100". – Alexandre Santos Jul 21 '14 at 23:18
  • 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. – Braj Jul 21 '14 at 23:21
  • @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. – Bohemian Jul 21 '14 at 23:22
  • @Braj that's not how I read the question: there are only 0 chars in his example input, so we can't be sure. – Bohemian Jul 21 '14 at 23:24
  • yes that's why I don't want to delete my post to make it helpful for others. – Braj Jul 21 '14 at 23:29
  • Well to put it simply, 0000 is a value of 0. 1111 is a value of 256. Im showing 0000 as a string but in decimal form, not binary form. I mean the String says its decimal like you could have 0002 and it would be 2 but I want it to be parsed and written as the binary value of 0000. If the String was 1111 I would want the value to be 1111 (in binary). – AMDG Jul 21 '14 at 23:31
  • @LinkTheProgrammer Why is `1111` a value of 256? – ajb Jul 21 '14 at 23:33
  • @LinkTheProgrammer what will be value of 1111 in binary? First tell me in what base it is? – Braj Jul 21 '14 at 23:34
  • 1111 is the binary value for the decimal 256. – AMDG Jul 21 '14 at 23:37
  • @Braj does base even matter? 1111 is binary, which is the integer value 256. I don't see what the confusion is... really. – AMDG Jul 21 '14 at 23:42
  • @LinkTheProgrammer You may want to check your math. – ajb Jul 21 '14 at 23:43
  • I never wanted to get into math!!! I just wanted a way to put binary in string form, into a binary file!!! – AMDG Jul 22 '14 at 00:13
  • How on earth can 1111 be 256? 1111 is 15 decimal. 11111111 is 255. Please explain what the conversion is. (btw, you probably shouldn't call your string variable `binary` if it's decimal) – Bohemian Jul 22 '14 at 00:13
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");
Igor Maznitsa
  • 833
  • 7
  • 12