How to convert a string to a stream of bits in java

后端 未结 3 2065
感动是毒
感动是毒 2021-02-15 17:24

How to convert a string to a stream of bits zeroes and ones what i did i take a string then convert it to an array of char then i used method called forDigit(char,int) ,but it

3条回答
  •  耶瑟儿~
    2021-02-15 18:04

    String strToConvert = "abc";
    byte [] bytes = strToConvert.getBytes();
    StringBuilder bits = new StringBuilder(bytes.length * 8); 
    System.err.println(strToConvert + " contains " + bytes.length +" number of bytes");
    for(byte b:bytes) {
      bits.append(Integer.toString(b, 2));
    }   
    
    System.err.println(bits);
    char [] chars = new char[bits.length()];
    bits.getChars(0, bits.length(), chars, chars.length);
    

提交回复
热议问题