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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.