Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
How about using String(byte[] bytes, String charsetName) Constructs a new String by decoding the specified array of bytes using the specified charset. –  Ingo Jan 22 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.) –  Hot Licks Jan 22 at 17:50
1  
This question appears to be off-topic because it is about not having studied the constructors of String –  Ingo Jan 22 at 17:50

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer

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.

share|improve this answer
    
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 Jan 23 at 9:51
    
@Ingo - It works in Java 6 too - docs.oracle.com/javase/6/docs/api/java/lang/…. And probably earlier. –  Stephen C Jan 23 at 11:02
    
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. –  Ingo Jan 23 at 11:07

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.