Binary to text in Java

88,593

Solution 1

You can use Integer.parseInt with a radix of 2 (binary) to convert the binary string to an integer:

int charCode = Integer.parseInt(info, 2);

Then if you want the corresponding character as a string:

String str = new Character((char)charCode).toString();

Solution 2

This is my one (Working fine on Java 8):

String input = "01110100"; // Binary input as String
StringBuilder sb = new StringBuilder(); // Some place to store the chars

Arrays.stream( // Create a Stream
    input.split("(?<=\\G.{8})") // Splits the input string into 8-char-sections (Since a char has 8 bits = 1 byte)
).forEach(s -> // Go through each 8-char-section...
    sb.append((char) Integer.parseInt(s, 2)) // ...and turn it into an int and then to a char
);

String output = sb.toString(); // Output text (t)

and the compressed method printing to console:

Arrays.stream(input.split("(?<=\\G.{8})")).forEach(s -> System.out.print((char) Integer.parseInt(s, 2))); 
System.out.print('\n');

I am sure there are "better" ways to do this but this is the smallest one you can probably get.

Solution 3

I know the OP stated that their binary was in a String format but for the sake of completeness I thought I would add a solution to convert directly from a byte[] to an alphabetic String representation.

As casablanca stated you basically need to obtain the numerical representation of the alphabetic character. If you are trying to convert anything longer than a single character it will probably come as a byte[] and instead of converting that to a string and then using a for loop to append the characters of each byte you can use ByteBuffer and CharBuffer to do the lifting for you:

public static String bytesToAlphabeticString(byte[] bytes) {
    CharBuffer cb = ByteBuffer.wrap(bytes).asCharBuffer();
    return cb.toString();
}

N.B. Uses UTF char set

Alternatively using the String constructor:

String text = new String(bytes, 0, bytes.length, "ASCII");

Solution 4

public static String binaryToText(String binary) {
    return Arrays.stream(binary.split("(?<=\\G.{8})"))/* regex to split the bits array by 8*/
                 .parallel()
                 .map(eightBits -> (char)Integer.parseInt(eightBits, 2))
                 .collect(
                                 StringBuilder::new,
                                 StringBuilder::append,
                                 StringBuilder::append
                 ).toString();
}

Solution 5

Here is the answer.

private String[] splitByNumber(String s, int size) {
    return s.split("(?<=\\G.{"+size+"})");
}
Share:
88,593
Nick
Author by

Nick

Updated on July 09, 2022

Comments

  • Nick
    Nick almost 2 years

    I have a String with binary data in it (1110100) I want to get the text out so I can print it (1110100 would print "t"). I tried this, it is similar to what I used to transform my text to binary but it's not working at all:

        public static String toText(String info)throws UnsupportedEncodingException{
            byte[] encoded = info.getBytes();
            String text = new String(encoded, "UTF-8");
            System.out.println("print: "+text);
            return text;
        }
    

    Any corrections or suggestions would be much appreciated.

    Thanks!

  • Nick
    Nick over 13 years
    Thank you, it makes a lot of sense, but I don't understand how to make a new Character from an int (charCode)
  • casablanca
    casablanca over 13 years
    @Nick: Just cast it to char.
  • S Gaber
    S Gaber about 12 years
    can i name this thing as a vector representation of text ?
  • Suvrat Apte
    Suvrat Apte about 2 years
    Does this work when the word in the byte is "0000" ?