0

How to make string "01001000"(for example) to byte and convert it to string.

Example :

if string = "0110000101100010" then output must be "ab"

because a == 01100001 and b == 01100010

1
  • 1
    www.google.com/search?q=binary+string+to+byte Commented Aug 31, 2011 at 13:20

2 Answers 2

4

something like this:

      String[] array = {"01100001","01100010"};
      StringBuilder sb = new StringBuilder();
      for( String string : array ) {
          sb.append( (char)Integer.parseInt( string, 2 ) );
      }

or if you have one String which has exact 8-bit * x letter.

    String source = "0110000101100010";
    StringBuilder sb = new StringBuilder();
    for( int i = 0; i < source.length(); i= i+8 ) {
        sb.append( (char)Integer.parseInt( source.substring( i, i+8 ), 2 ) );
    }
Sign up to request clarification or add additional context in comments.

Comments

1
StringBuilder sb = new StringBuilder();
for(String str : "0110000101100010".split("(?<=\\G.{8})")){
    sb.append((char)Byte.parseByte(str,2));
}
System.out.println(sb.toString());

Will output --> ab

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.