# Thread: ASCII to binary code

1. Member
Join Date
Dec 2010
Posts
8
Rep Power
0

## ASCII to binary code

Hi, I've googled, but can't find much.

I have to write method that, takes a char and transform it to reverse bits

public static char reverseBits(char number)

example:

System.out.println(class_name.reverseBits('A'));

Result:

1000001000000000

This is the ascii code of an 'A'
0000000001000001 - char 'A'

It's easy to change int, double or any number to bin, oct, hex.

But my question is, how to change any char to ascii code ?

2. Originally Posted by Vagabond.drv
Hi, I've googled, but can't find much.

I have to write method that, takes a char and transform it to reverse bits

public static char reverseBits(char number)

example:

System.out.println(class_name.reverseBits('A'));

Result:

1000001000000000

This is the ascii code of an 'A'
0000000001000001 - char 'A'

It's easy to change int, double or any number to bin, oct, hex.

But my question is, how to change any char to ascii code ?
A char is nothing more than a small (unsigned) int. The Integer class can be of help here, e.g. Integer.toString('A', 2) gives you the binary representation of the character 'A'. The StringBuilder class can be of help if you want to reverse the String.

kind regards,

Jos

3. Senior Member
Join Date
Dec 2010
Posts
100
Rep Power
0
Hi - I think this is pretty simple. You already know how to convert from char to int value right, by simply using a cast:
Java Code:
```char A = 'A';
int asciiValue = (int) A;```
Then you can use the toBinaryString() method in the Integer class to convert your int to ASCII binary value. THe syntax for this method is:
Java Code:
`Integer.toBinaryString(int);`
You can read more from the API for Integer class: Integer (Java 2 Platform SE 5.0)

Best,

4. Originally Posted by user0
Hi - I think this is pretty simple. You already know how to convert from char to int value right, by simply using a cast:
Java Code:
```char A = 'A';
int asciiValue = (int) A;```
That explicit cast is not needed because it is a so called 'widening cast' where no bits will be lost because of the cast. The compiler arranges everything for you:

Java Code:
`int a= 'A'; // a char assigned to an int, no probelm`
kind regards,

Jos

5. Senior Member
Join Date
Dec 2010
Posts
100
Rep Power
0
Originally Posted by JosAH
That explicit cast is not needed because it is a so called 'widening cast' where no bits will be lost because of the cast. The compiler arranges everything for you:

Java Code:
`int a= 'A'; // a char assigned to an int, no probelm`
@ Jos - I never knew that, and now I do. Thanks for pointing that out Jos!

Best,

6. Originally Posted by user0
@ Jos - I never knew that, and now I do. Thanks for pointing that out Jos!
You're welcome of course; most people with a (Visual?) Basic background and new to Java think something special of characters; most of the time they think of them as single character Strings while all they are is just a small unsigned int. It's the compiler that allows a symbolic notation, i.e. when it parses 'A' it translates it to that small int 65. Converting to/from ASCII code (better, Unicode) and chars is trivial: there's nothing to be done ;-)

kind regards,

Jos

7. Member
Join Date
Dec 2010
Posts
8
Rep Power
0
Thx guys everything works great :)

But have one more question, I have to do it in a 16 bits.
0000000001000001 - char 'A'
It is possible to get 16-bit length or add those zeroes manually ?

8. Originally Posted by Vagabond.drv
Thx guys everything works great :)

But have one more question, I have to do it in a 16 bits.
0000000001000001 - char 'A'
It is possible to get 16-bit length or add those zeroes manually ?
Yup, you have to do it yourself but it's easy: prepend your String with 15 zeros and take the last 16 chars (hint: substring( ... ) and length()) of the result. Alternatively you have to fiddle a bit with a StringBuilder/StringBuffer.

kind regards,

Jos

9. Yeah, leading 0's are always implied. Write a simple method using a loop to add missing zeros on a case by case basis :D

10. Member
Join Date
Dec 2010
Posts
8
Rep Power
0
Thx all, I really have one last question in this thread :P

When I reverse
0000000001000001 - char 'A'

to

1000001000000000

How to convert this code to ascii char ?

I know, in this example the result will be nonsense :P

Thx again :)

11. Originally Posted by Vagabond.drv
Thx all, I really have one last question in this thread :P

When I reverse
0000000001000001 - char 'A'

to

1000001000000000

How to convert this code to ascii char ?

I know, in this example the result will be nonsense :P

Thx again :)
Have a look at the Integer class; it has static methods that can parse String representations of ints. If you need a char as a result you have to cast it back to a char again: char c= (char)intValue;

kind regards,

Jos

12. Member
Join Date
Dec 2010
Posts
8
Rep Power
0
Oh yeah, you have posted it before :P

Sry, for bothering and thx again. I also have learnt to read documentation, before ask questions :)

13. I assume you found the how to specify a base for binary String parsing (Integer.parseInt(myString, 2) //2 for binary. The regular ASCII table only goes to 128 and the extended one goes to 256. Java uses Unicode which goes way beyond, but requires special notation.

#### Posting Permissions

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