Results 1 to 13 of 13
Thread: ASCII to binary code
- 01-12-2011, 03:33 PM #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 ?
- 01-12-2011, 03:38 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-12-2011, 03:52 PM #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:
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:char A = 'A'; int asciiValue = (int) A;
You can read more from the API for Integer class: Integer (Java 2 Platform SE 5.0)Java Code:Integer.toBinaryString(int);
Best,--user0--
- 01-12-2011, 05:08 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-12-2011, 05:50 PM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
- 01-12-2011, 06:47 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-12-2011, 06:54 PM #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 ?
- 01-12-2011, 07:04 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-12-2011, 09:28 PM #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
- 01-13-2011, 09:49 AM #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 :)
- 01-13-2011, 10:07 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-13-2011, 10:33 AM #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 :)
- 01-13-2011, 01:52 PM #13
Similar Threads
-
Binary and ASCII
By mac in forum New To JavaReplies: 4Last Post: 01-10-2010, 06:31 PM -
Code for image to binary
By Deva in forum New To JavaReplies: 3Last Post: 12-24-2009, 04:48 PM -
code for binary tree in jsp
By ajay kumar in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11-17-2009, 06:45 AM -
Ascii code.........
By Somitesh Chakraborty in forum New To JavaReplies: 6Last Post: 11-04-2008, 05:32 PM -
How to obtain ASCII code of a character
By karma in forum New To JavaReplies: 4Last Post: 07-20-2008, 02:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks