hi ..
i wanna read text file in binary format ...
4 ex : lele and raz ..."in text file "
l=10011001 e= 011110011 ..... "cmd (8 bit per letter)"
and but them in an array ...i tried the type StringBuilder but it didnt work ...
i need a method ...
thnx

hi ..
i wanna read text file in binary format ...
4 ex : lele and raz ..."in text file "

      l=10011001    e= 011110011 ..... "cmd  (8 bit per letter)"

and but them in an array ...i tried the type StringBuilder but it didnt work ...
i need a method ...
thnx

/*In this below coding is used for convert text to binary code */



import java.io.*;

public class TexttoBinary {
  private static final int maxBytes = 3;
  public static void main(String[] args) {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    do {
      try {
        System.out.print("Type the number to parse: ");
        int number = Integer.parseInt(in.readLine());
        int Bit;
        String result = "";
        for (int i = maxBytes*8; i >= 0; i--) {
      Bit = 1 << i;
      if (number >= Bit) {
            result += 1;
            number -= Bit;
          }
          else {
            result += 0;
          }
        }
        System.out.println(result);
      }
    catch (NumberFormatException e) {
      System.exit(0);
      }
      catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
        }
        }
        while (true);
        }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

thnx vm ...but the text is written in notepad ...so i have to import the text then deal with it ...

start quote:

/*In this below coding is used for convert text to binary code */



import java.io.*;

public class TexttoBinary {
  private static final int maxBytes = 3;
  public static void main(String[] args) {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    do {
      try {
        System.out.print("Type the number to parse: ");
        int number = Integer.parseInt(in.readLine());
        int Bit;
        String result = "";
        for (int i = maxBytes*8; i >= 0; i--) {
      Bit = 1 << i;
      if (number >= Bit) {
            result += 1;
            number -= Bit;
          }
          else {
            result += 0;
          }
        }
        System.out.println(result);
      }
    catch (NumberFormatException e) {
      System.exit(0);
      }
      catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
        }
        }
        while (true);
        }
}

end quote.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.