【问题标题】:Binary to text in JavaJava中的二进制到文本
【发布时间】:2010-11-18 04:34:39
【问题描述】:

我有一个包含二进制数据的字符串 (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;
    }

任何更正或建议将不胜感激。

谢谢!

【问题讨论】:

    标签: java string encoding utf-8 nsstringencoding


    【解决方案1】:

    这是我的(在 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');
    

    我确信有“更好”的方法可以做到这一点,但这是你可能得到的最小的方法。

    【讨论】:

      【解决方案2】:
      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();
      }
      

      【讨论】:

        【解决方案3】:

        我知道 OP 声明他们的二进制文件是 String 格式,但为了完整起见,我想我会添加一个解决方案,直接从 byte[] 转换为字母字符串表示。

        正如 casablanca 所说,您基本上需要获得字母字符的数字表示。如果您尝试转换比单个字符更长的任何内容,它可能会以 byte[] 的形式出现,而不是将其转换为字符串,然后使用 for 循环附加每个 byte 的字符,您可以使用 ByteBufferCharBuffer 为你做起重:

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

        注意使用 UTF 字符集

        或者使用 String 构造函数:

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

        【讨论】:

          【解决方案4】:

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

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

          那么如果你想要对应的字符为字符串:

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

          【讨论】:

          • 谢谢,很有道理,但我不明白如何从 int (charCode) 中创建新字符
          • @Nick:把它投给char
          • 我能把这个东西命名为文本的向量表示吗?
          • 当字节中的单词是“0000”时这是否有效?
          【解决方案5】:

          这就是答案。

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

          【讨论】:

            【解决方案6】:

            反过来(“info”是输入文本,“s”是二进制版本)

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

            【讨论】:

              【解决方案7】:

              查看parseInt 函数。您可能还需要演员表和 Character.toString 函数。

              【讨论】:

                【解决方案8】:

                您也可以使用没有流和正则表达式的替代解决方案 (based on casablanca's answer):

                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指定的字符作为字符串转字符序列即可。

                【讨论】:

                  猜你喜欢
                  • 2013-12-29
                  • 1970-01-01
                  • 2015-04-26
                  • 2011-10-30
                  • 2012-05-04
                  • 2016-07-31
                  • 1970-01-01
                  • 2010-10-11
                  • 1970-01-01
                  相关资源
                  最近更新 更多