Results 1 to 6 of 6
  1. #1
    akira220984 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default Another ascii question....

    Hi guys,

    Firstly I would like to say hello as this is my first post.

    Secondly, I would like to apologise for this thread about ascii, but its bothering me.

    I've written this:

    byte[] x = message.getBytes();
    for(int i = 0; i < x.length; i++)
    {
    mess += Integer.toString(x[i]);
    }

    which takes a String parameter and returns the ascii value, so, "A" would return 65, "AA" would return 6565 etc

    My question is this: Taking the latter of the two examples above, how would I return 6565 to "AA".

    It's quite late at night to be dealing with this but I'm stuck and cant sleep so thought I would give it a go with no success at this point in time.

    I look forward to anyone who can shed some light on this.

    Regards

    Ric

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you'll need to change the string into a byte, then typecast it into a char.
    Java Code:
        String msg = "65";
        char c = (char) Byte.parseByte(msg);
        System.out.println(c);
    in your case, i would put that in a loop and pick out every two digits.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I don't think it will work with base 10 numbers as some ascii numbers are 3 digits long, and without spaces or something else to allow parsing of the string, it would be easy to get out of phase. One solution is to use hex Strings which will be 2 digits, another is to separate each number by a space.

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you're right, i was thinking of hex. but 65 is dec.
    so you could use hex or write a checker.
    Codes 33 to 126, known as the printable characters, represent letters, digits, punctuation marks, and a few miscellaneous symbols.
    so check the first digit, if its 3 or above then take two digits. if its one, then take 3 digits.
    e.g.: 33366578125122 would be split into
    33, 36, 65, 78, 125, 122.
    and then typecast into char.
    Last edited by angryboy; 02-19-2009 at 05:42 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    akira220984 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Thank you very much for your quick replies.

    It must be one of those things where you are looking at something for a very long time, but I still cannot do it!

    I have amended:

    mess += Integer.toString(x[i]);

    to

    mess += Integer.toString(x[i]).toUpperCase();

    so that most values only consist of 2 values.

    But I cannot loop over 2 characters at a time and change them back to chars :(

    It's driving me mad!

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    how did you decode the digits? lets see the code.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. ASCII to EBCDIC conversion
    By satish kumar in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2013, 01:50 PM
  2. I need help with ascii characters
    By Grandon in forum Eclipse
    Replies: 17
    Last Post: 11-08-2008, 02:12 AM
  3. Ascii code.........
    By Somitesh Chakraborty in forum New To Java
    Replies: 6
    Last Post: 11-04-2008, 05:32 PM
  4. Getting ASCII codes of character
    By gapper in forum New To Java
    Replies: 1
    Last Post: 02-02-2008, 09:42 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
  •