Results 1 to 13 of 13
  1. #1
    Vagabond.drv is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Vagabond.drv View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    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,
    --user0--

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by user0 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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,
    --user0--

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by user0 View Post
    @ 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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Vagabond.drv is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    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. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Vagabond.drv View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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. #10
    Vagabond.drv is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    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. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Vagabond.drv View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Vagabond.drv is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    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. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Binary and ASCII
    By mac in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 06:31 PM
  2. Code for image to binary
    By Deva in forum New To Java
    Replies: 3
    Last Post: 12-24-2009, 04:48 PM
  3. code for binary tree in jsp
    By ajay kumar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-17-2009, 06:45 AM
  4. Ascii code.........
    By Somitesh Chakraborty in forum New To Java
    Replies: 6
    Last Post: 11-04-2008, 05:32 PM
  5. How to obtain ASCII code of a character
    By karma in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 02:57 AM

Posting Permissions

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