Binary to text in Java

泪湿孤枕 提交于 2019-11-26 02:38:29

问题


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!


回答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();



回答2:


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");



回答3:


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.




回答4:


Here is the answer.

private String[] splitByNumber(String s, int size) {
    return s.split("(?<=\\G.{"+size+"})");
}



回答5:


The other way around (Where "info" is the input text and "s" the binary version of it)

byte[] bytes = info.getBytes();
BigInteger bi = new BigInteger(bytes);
String s = bi.toString(2); 



回答6:


Look at the parseInt function. You may also need a cast and the Character.toString function.




回答7:


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();
}


来源:https://stackoverflow.com/questions/4211705/binary-to-text-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!