6

as the title says, how do I do it? Its easy to convert from string -> byte -> string binary, But how do I convert back? Below is a example. The output is : 'f' to binary: 01100110 294984

I read somewhere that I could use the Integer.parseInt but clearly that is not the case :( Or am I doing something wrong?

Thanks, :)

public class main{
    public static void main(String[] args) {

         String s = "f";
          byte[] bytes = s.getBytes();
          StringBuilder binary = new StringBuilder();
          for (byte b : bytes)
          {
             int val = b;
             for (int i = 0; i < 8; i++)
             {
                binary.append((val & 128) == 0 ? 0 : 1);
                val <<= 1;
             }
             binary.append(' ');
          }
          System.out.println("'" + s + "' to binary: " + binary);

        System.out.println(Integer.parseInt("01100110", 2));
    }
}
1
  • You desperately need to clarify your requirement. Commented Sep 4, 2013 at 20:10

3 Answers 3

16

You can use Byte.parseByte() with a radix of 2:

byte b = Byte.parseByte(str, 2);

Using your example:

System.out.println(Byte.parseByte("01100110", 2));
102
Sign up to request clarification or add additional context in comments.

4 Comments

Except that the OP (if he is to be believed) wants to convert to binary text string.
(Though on re-reading, it's impossible to tell what the OP wants.)
That doesn't work for values greater than 01111111 == 127 as Byte has a range from -128 to 127.
@thomas.mc.work Right but the OP just wants bytes; reread the question.
1

You can parse it to an integer in base 2, and convert to a byte array. In your example you've got 16 bits you can also use short.

short a = Short.parseShort(b, 2);
ByteBuffer bytes = ByteBuffer.allocate(2).putShort(a);

byte[] array = bytes.array();

Just in case if you need it for a Very Big String.

String b = "0110100001101001";
byte[] bval = new BigInteger(b, 2).toByteArray();

1 Comment

Except that the OP (if he is to be believed) wants to convert to binary text string.
0

I made like this, converted a string s -> byte[] and then used Integer.toBinaryString to get binaryStringRep. I converted bianryStringRep by using Byte.parseByte to get the bianryStringRep into byte and the String(newByte[]) to get the byte[] into a String! Hope it helps others then me aswell! ^^

public class main{
    public static void main(String[] args) throws UnsupportedEncodingException {

         String s = "foo";
          byte[] bytes = s.getBytes();
          byte[] newBytes = new byte[s.getBytes().length];
          for(int i = 0; i < bytes.length; i++){
              String binaryStringRep = String.format("%8s", Integer.toBinaryString(bytes[i] & 0xFF)).replace(' ', '0');
              byte newByte = Byte.parseByte(binaryStringRep, 2);
              newBytes[i] = newByte;
          }

        String str = new String(newBytes, "UTF-8");
        System.out.println(str);
    }
}

Comments

Your Answer

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.