1

I am trying to make a Java binary translator, and I have gotten to the part of translating the binary back to a String. I have made it to a byte array. I want to convert it to a string.

For example, I want a new byte array of {01000001, 01100001, 01000010, 01100010} to return "AaBb".

How can I do this?

3
  • How about using String(byte[] bytes, String charsetName) Constructs a new String by decoding the specified array of bytes using the specified charset. Commented Jan 22, 2014 at 17:49
  • (And, actually, if you already have the byte array, and not the binary bits in a character string or some such, you're done.) Commented Jan 22, 2014 at 17:50
  • 1
    This question appears to be off-topic because it is about not having studied the constructors of String Commented Jan 22, 2014 at 17:50

3 Answers 3

1

I think what you really want to do is convert a binary (decimal) number to its ASCII representation. If so, try something like this:

public class ByteArrayToAsciiChar {

  public String byteToCharacter(byte b) {
    return Character.valueOf((char)b).toString();
  }

  public static void main(String[] args) {
    byte[] byteArray = {
        0b00100101,
        0b01000001,
        0b01100001,
        0b01000010,
        0b01100010,
        0b01010101
    };

    ByteArrayToAsciiChar testClass = new ByteArrayToAsciiChar();

    for (byte b : byteArray) {
      System.out.println("Byte: " + b + " ==> " + testClass.byteToCharacter(b));
    }

  }

}

Which gives this output:

Byte: 37 ==> %
Byte: 65 ==> A
Byte: 97 ==> a
Byte: 66 ==> B
Byte: 98 ==> b
Byte: 85 ==> U

HTH

Sign up to request clarification or add additional context in comments.

Comments

1

Erm ... it is as simple as this:

    byte[] bytes = {0b01000001, 0b01100001, 0b01000010, 0b01100010};
    String str = new String(bytes, "ASCII");

Of course, that assumes that the bytes represent ASCII encoded characters. If not then use the name of the actual character encoding.

If you are going to do this a lot, then it is worth looking up the Charset object for the character set and using the String(byte[], Charset) overload.

3 Comments

Indeed, the latter is even better as you can be sure at compile time that the desired charset really exists. Yet it doesn't work only from Java7 on.
@Ingo - It works in Java 6 too - docs.oracle.com/javase/6/docs/api/java/lang/…. And probably earlier.
Oha! Silly me. But I remember in Java6 there were some constructors that only accept a charset name (and throw exceptions when the name is not correct) and not a charset. Maybe InputStreamReader or something like that.
0

If you already have a byte array (exemplarily constructed below), you can easily pass the byte array to a new String (as Ingo suggested) and viola - you're done.

byte b[] = new byte[4];
b[0] = Byte.parseByte("01000001", 2);
b[1] = Byte.parseByte("01100001", 2);
b[2] = Byte.parseByte("01000010", 2);
b[3] = Byte.parseByte("01100010", 2);

String output = new String(b);

If you only have an array of binarys in form of strings, you could use this:

public String convertBinaryArrayToString(String binary[]) {
    String ret = null;
    for (String i : binary) {
        ret += (char) Byte.parseByte(i, 2);
    }
    return ret;
}

Hope this helps.

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.