Results 1 to 5 of 5
  1. #1
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Converting characters to decimal to binary

    As you can see from the title I need to convert whatever text is put in to decimal ascii, and then convert it to binary. I figured out how to convert the text to ascii but I'm not sure how to then convert the ascii to binary. I'm fairly new so I'm not really looking for anything too complicated. Any help is appreciated though.

    Java Code:
            Scanner input = new Scanner(System.in);
    
            String textString = "";
    
            System.out.println("Enter an alpha-numeric string: ");
            textString = input.nextLine();
    
            for(int i = 0; i < textString.length(); i++)
            {
                System.out.printf("%s\n", (int)textString.charAt(i));
            }
    So putting in asdf would output 97 115 100 102.
    I was told I should use another for loop inside the first one or something like that to convert the ascii but I don't really know how to go about actually converting the ascii.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can get a binary String representation of an int using the Integer method Integer.toBinaryString(int). e.g.,

    Java Code:
          for (int i = 0; i < textString.length(); i++) {
             char myChar = textString.charAt(i);
             int myInt = (int)myChar;
             System.out.printf("%c: 0x%x, %s%n", myChar, myInt, Integer.toBinaryString(myInt));
          }

  3. #3
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Is there another way to do it other than the Integer.toBinaryString()? Thanks for the help btw. I really appreciate it.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Yes.

    Write your own code. It can be done using recursion and continaully dividing and modding the value by 2.

  5. #5
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    I didn't really get it by modding but I guess what I did works well enough, even if it is kinda stupid. So here's what I got now.

    Java Code:
    Scanner input = new Scanner(System.in);
    
            String textString = "";
            int sixtyFoursPlace = 0;
            int thirtyTwosPlace = 0;
            int sixteensPlace = 0;
            int eightsPlace = 0;
            int foursPlace = 0;
            int twosPlace = 0;
            int onesPlace = 0;
            int sum = 0;
    
            System.out.println("Enter an alpha-numeric string: ");
            textString = input.nextLine();
    
            for(int i = 0; i < textString.length(); i++)
            {
                char myChar = textString.charAt(i);
                int intChar = (int)textString.charAt(i);
    
                
                sixtyFoursPlace = intChar / 64;
                sum = 64 * sixtyFoursPlace;
                
                thirtyTwosPlace = (intChar - sum) / 32;
                sum += (32 * thirtyTwosPlace);
                
                sixteensPlace = (intChar - sum) / 16;
                sum += (16 * sixteensPlace);
    
                eightsPlace = (intChar - sum) / 8;
                sum += (8 * eightsPlace);
    
                foursPlace = (intChar - sum) / 4;
                sum += (4 * foursPlace);
    
                twosPlace = (intChar - sum) / 2;
                sum += (2 * twosPlace);
    
                onesPlace = (intChar - sum) / 1;
    
    
                System.out.printf("The character \"%c\"'s Ascii: %s Binary: %d%d%d%d%d%d%d\n", 
                        myChar, (int)textString.charAt(i), sixtyFoursPlace, thirtyTwosPlace,
                        sixteensPlace, eightsPlace, foursPlace, twosPlace, onesPlace);
    
                
            }
    I was also wanting to try to turn binary put in back to ascii and into text again. Would it be easier if I tried the converting to binary by modding by 2 and went from there? I'm not really sure how to do that though...Any more help is still appreciated.

Similar Threads

  1. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  2. Algorithm for converting binary/hex to decimal
    By addictz04 in forum New To Java
    Replies: 2
    Last Post: 11-29-2010, 06:49 PM
  3. Converting whole number into decimal
    By jim01 in forum New To Java
    Replies: 2
    Last Post: 09-23-2010, 07:58 PM
  4. converting a decimal to an int
    By shuks in forum New To Java
    Replies: 9
    Last Post: 10-12-2009, 09:41 AM
  5. Replies: 3
    Last Post: 07-25-2009, 01:44 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
  •