Results 1 to 3 of 3
  1. #1
    aelynne is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default convert 30 char of ascii to binary

    How to make a program that can accept 30 char of ascii than convert it into binary?

    Java 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);
      }
    }
    I already done this but this only can accept number not letter. Which part that i need to change?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: convert 30 char of ascii to binary

    I already done this but this only can accept number not letter. Which part that i need to change?
    To be more precise your program accepts a string from the user and uses this to create an integer number which it converts.

    There is no reason why you could not just read() rather than readLine(). This would give you an int directly without the need to parse anything, and the result string can be built up as you are already doing.

    Of course this would convert only a single character - so you would need to put the whole process inside a loop so that it is done 30 times.

    -----

    You should follow Java coding style and (1) indent the code, and (2) Start the variable holding the current bit value with a lower case letter: bit.

    "result+=0" had me confused for a moment. It would be far more readable if the string containing a zero digit were written as a string to make it clear you mean to concatenate rather than increment.

    Java Code:
    result += "0";
    "while(true){...}" is more readable to my eye than "do{...}while(true)" - but that may be just me. I like to see the important loop bits "upfront" and, so, prefer while and if to do.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: convert 30 char of ascii to binary

    I already done this
    Is this your code? (Hint: I know the answer, but others here deserve to know as well.)

    Please note that RoseIndia not a good site. You might do better to throw away what you have and start afresh with your own code.

Similar Threads

  1. How to convert BCD to ascii
    By slavicp in forum Advanced Java
    Replies: 2
    Last Post: 04-05-2011, 12:54 PM
  2. How to convert BCD to ascii
    By slavicp in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 12:53 PM
  3. ASCII to binary code
    By Vagabond.drv in forum New To Java
    Replies: 12
    Last Post: 01-13-2011, 02:52 PM
  4. convert String to ASCII value
    By uthpalaw in forum New To Java
    Replies: 19
    Last Post: 09-17-2010, 04:02 PM
  5. Binary and ASCII
    By mac in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 07:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •