title: How can i convert character 0s and 1s read from a txt File to byte?
body: <p>I want to read a txt file containing 0s and 1s , and convert them to bytes but first convert to binary. e.g textFile contains "00101001". Every character is 1 byte , after reading from file i get 8 bytes. Now i want to append them to a String and convert to byte. I should get 1 byte from 8 bytes.Is that right? </p>
  
  <p>is there is a better option?.
  What i have did so far:</p>
  
  <pre><code>public byte[] convertBytes(String File1, int length) throws IOException
  {
  
      byte BytesfromFile[]=readInput(File1,length);          // readinput() returns (length*8) bytes,contains 48 or 49s.
      StringBuilder stringBuilder = new StringBuilder();             //Stringbuilder append chars to build String
      byte convertedBytes[]=new byte[BytesfromFile.length/8];            //size of byte array init
      int stringToInt;
      byte intToByte;
  
      for(int index=0,bitcounter=0,indexCoverted=0;index&lt;BytesfromFile.length;index++,++bitcounter)
      {
  
          stringBuilder.append((char) BytesfromFile[index]); //append 48 or 49 casted to char
  
          if(bitcounter==8)
          {   String Binary = stringBuilder.toString();   // if 8 bits appended, toString
              stringToInt = Integer.parseInt(Binary, 2);  // convert Binary String to Int
              intToByte = (byte)stringToInt;              // cast Int to byte
              convertedBytes[indexCoverted++]=intToByte;
              bitcounter=0;
              stringBuilder.setLength(0);                 //set length to 0, to append next 8 bit.
          }
      }
              return convertedBytes;
  }
  </code></pre>