# ASCII to binary code

• 01-12-2011, 04:33 PM
Vagabond.drv
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 ?
• 01-12-2011, 04:38 PM
JosAH
Quote:

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
• 01-12-2011, 04:52 PM
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:
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:
Code:

`Integer.toBinaryString(int);`
You can read more from the API for Integer class: Integer (Java 2 Platform SE 5.0)

Best,
• 01-12-2011, 06:08 PM
JosAH
Quote:

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

Code:

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

Jos
• 01-12-2011, 06:50 PM
user0
Quote:

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:

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,
• 01-12-2011, 07:47 PM
JosAH
Quote:

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
• 01-12-2011, 07:54 PM
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 ?
• 01-12-2011, 08:04 PM
JosAH
Quote:

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
• 01-12-2011, 10:28 PM
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
• 01-13-2011, 10:49 AM
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 :)
• 01-13-2011, 11:07 AM
JosAH
Quote:

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
• 01-13-2011, 11:33 AM
Vagabond.drv
Oh yeah, you have posted it before :P

Sry, for bothering and thx again. I also have learnt to read documentation, before ask questions :)
• 01-13-2011, 02:52 PM