Java中的二进制转文本

10

我有一个包含二进制数据的字符串(1110100),我想要将其转换为文本以便打印(1110100会打印出“t”)。我尝试了以下代码,它与我用来将文本转换为二进制的代码类似,但根本不起作用:

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

任何更正或建议都将不胜感激。
谢谢!
8个回答

29

您可以使用Integer.parseInt和2(二进制)作为基数将二进制字符串转换为整数:

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

如果您想要相应的字符作为字符串,则可以这样做:
String str = new Character((char)charCode).toString();

1
谢谢,这很有道理,但我不明白如何从int(charCode)创建一个新的字符。 - Nick
3
将其转换为 char 类型。 - casablanca
我可以把这个东西称为文本的向量表示吗? - S Gaber
1
当字节中的单词为“0000”时,这是否有效? - Suvrat Apte

9

这是我的一个示例(在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)

以及压缩方法打印到控制台:

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

我相信有更好的方法来实现这个,但这可能是你能找到的最小的方法。


5
我知道楼主说他们的二进制数据是以String形式呈现的,但为了完整性,我想添加一种直接从byte[]转换为字母字符串表示的解决方案。
正如casablanca所述,您需要获得字母字符的数字表示。如果您尝试转换长度超过一个字符的任何内容,它可能会作为byte[]出现,而不是将其转换为字符串,然后使用for循环附加每个byte的字符,您可以使用ByteBufferCharBuffer来完成此操作:
public static String bytesToAlphabeticString(byte[] bytes) {
    CharBuffer cb = ByteBuffer.wrap(bytes).asCharBuffer();
    return cb.toString();
}

注意:使用UTF字符集

或者使用字符串构造函数:

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

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

1
另一种方法是(其中“info”是输入文本,“s”是其二进制版本)
byte[] bytes = info.getBytes();
BigInteger bi = new BigInteger(bytes);
String s = bi.toString(2); 

1

这里是答案。

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

0

0

此外,您还可以使用不涉及流和正则表达式的替代解决方案(基于卡萨布兰卡的答案):

public static String binaryToText(String binaryString) {
    StringBuilder stringBuilder = new StringBuilder();
    int charCode;
    for (int i = 0; i < binaryString.length(); i += 8) {
        charCode = Integer.parseInt(binaryString.substring(i, i + 8), 2);
        String returnChar = Character.toString((char) charCode);
        stringBuilder.append(returnChar);
    }
    return stringBuilder.toString();
}

你只需要将指定的字符作为字符串附加到字符序列中即可append


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接