Results 1 to 5 of 5
- 02-02-2011, 09:36 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
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.
So putting in asdf would output 97 115 100 102.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)); }
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.
-
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)); }
- 02-03-2011, 06:02 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Is there another way to do it other than the Integer.toBinaryString()? Thanks for the help btw. I really appreciate it.
- 02-03-2011, 10:32 PM #4
Yes.
Write your own code. It can be done using recursion and continaully dividing and modding the value by 2.
- 02-04-2011, 11:06 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
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.
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.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); }
Similar Threads
-
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12-26-2010, 09:59 AM -
Algorithm for converting binary/hex to decimal
By addictz04 in forum New To JavaReplies: 2Last Post: 11-29-2010, 06:49 PM -
Converting whole number into decimal
By jim01 in forum New To JavaReplies: 2Last Post: 09-23-2010, 07:58 PM -
converting a decimal to an int
By shuks in forum New To JavaReplies: 9Last Post: 10-12-2009, 09:41 AM -
converting decimal to binary value using recursion in java
By Anindo in forum New To JavaReplies: 3Last Post: 07-25-2009, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks