# Thread: Converting characters to decimal to binary

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.

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. 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. 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.

4. Yes.

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

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.

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.

#### Posting Permissions

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