Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: How to convert from characters to ASCII and ASCII to binary?

  1. #1
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default How to convert from characters to ASCII and ASCII to binary?

    The program I am creating first asks the user to write down fifteen characters. Once the user does so, it is supposed to convert them to ASCII number system (in which a is 65, b is 66, c is 67, etc.) and output the values. Then, it is supposed to convert those values to the binary number system.

    Does anyone know what code to use to do this? Try to make code that's like this one:

    for ( int i = 0; i < 255; i++){

    System.out.println(i + " " + (char)i);

    }

    The code above lists all the numbers from 0 to 255 with their values in the ASCII number system. If possible use this code to convert the letters the user types.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to convert from characters to ASCII and ASCII to binary?

    convert the letters the user types.
    Everything in a computer is in binary. What you see depends on how you view it. You've noticed that the int value 65 can be viewed as the char 'a'.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to convert from characters to ASCII and ASCII to binary?

    You can "display" character input in any base. Check out the Integer class for ways to represent the character in different bases.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: How to convert from characters to ASCII and ASCII to binary?

    Quote Originally Posted by Norm View Post
    Everything in a computer is in binary. What you see depends on how you view it. You've noticed that the int value 65 can be viewed as the char 'a'.
    That's not what I mean. In Netbeans the user clicks the play button. Then they type any 15 characters. Those are supposed to stay there, but when the user has finished typing I want whatever they wrote written again in the ASCII number system. For example, if they write cat then after cat I want displayed 67 65 84. Similarly I want the numbers converted to binary system. So if the user types cat this is what they should see when all the conversions are done (note that I am not quite sure I have converted to the binary system properly below) :
    cat
    67 65 84
    01000011 01000001 01010100

    As you can see the only difference is that in my program the user would type 15 characters.
    Last edited by Anne; 08-13-2015 at 12:40 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to convert from characters to ASCII and ASCII to binary?

    if they write cat then after cat I want displayed 67 65 84.
    Look at using casting on the char values to have the value displayed as int: (int)'a' displays as 97
    See the Integer class for methods for converting int values to Strings in different bases.

    Note: (int)'A' is 65
    Tolls likes this.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to convert from characters to ASCII and ASCII to binary?

    Quote Originally Posted by Anne View Post
    That's not what I mean. In Netbeans the user clicks the play button. Then they type any 15 characters. Those are supposed to stay there, but when the user has finished typing I want whatever they wrote written again in the ASCII number system. For example, if they write cat then after cat I want displayed 67 65 84. Similarly I want the numbers converted to binary system. So if the user types cat this is what they should see when all the conversions are done (note that I am not quite sure I have converted to the binary system properly below) :
    cat
    67 65 84
    1000011 1000001 1010100

    As you can see the only difference is that in my program the user would type 15 characters.
    64+2+1 64+1 64+16+4
    Looks good to me.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 10
    Last Post: 10-03-2013, 11:51 AM
  2. convert 30 char of ascii to binary
    By aelynne in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 09:15 AM
  3. Replies: 0
    Last Post: 02-22-2011, 08:27 AM
  4. Non-ASCII Characters
    By AJArmstron@aol.com in forum New To Java
    Replies: 6
    Last Post: 05-06-2010, 10:16 PM
  5. I need help with ascii characters
    By Grandon in forum Eclipse
    Replies: 17
    Last Post: 11-08-2008, 02:12 AM

Tags for this Thread

Posting Permissions

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