Results 1 to 9 of 9
  1. #1
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Thumbs up help - Ascii to hex and hex to ascii conversion

    Hi friends,

    I need to convert the hex data to ascii java.. commonly, the Hex values are 0 (00) to 127 (7F) are converted and printed correctly, i need to print the value from 128 (80) - 255 (FF)..

    How to convert the value.. I tried lot.. any one can help me..

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    What have you tried, and what were the results?

  3. #3
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    private static String asciitohex(String data)
    {
    char[] chars = data.toCharArray();
    StringBuffer hex = new StringBuffer();
    StringBuffer hexval = new StringBuffer();
    for(char ch : chars)
    {
    hex.append(Integer.toHexString(ch));
    }
    return hexval.toString();
    }

    here is my code friend to convert ascii to hex,
    it converts 'R' to '52'..
    some it will shows 2018, 201C, 2026, FFFD, 153, 178...
    How can i get know the ascii values for these hex values.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    Do you have some examples of what is converted to the longer hex values?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rjagan View Post
    private static String asciitohex(String data)
    {
    char[] chars = data.toCharArray();
    StringBuffer hex = new StringBuffer();
    StringBuffer hexval = new StringBuffer();
    for(char ch : chars)
    {
    hex.append(Integer.toHexString(ch));
    }
    return hexval.toString();
    }

    here is my code friend to convert ascii to hex,
    it converts 'R' to '52'..
    some it will shows 2018, 201C, 2026, FFFD, 153, 178...
    How can i get know the ascii values for these hex values.
    Those are Unicode values; most of them are out of the range of ASCII values but the first 128 [0 ... 127] values are equal in Unicode and ASCII. Don't bother about ASCII, it is old fashioned, work with Unicode code points only.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    i use these codes to my serial port connection ..
    am sending the hex value 31... and the serial port will show result as 1..
    then i convert the ascii value to 31..
    like these,
    32 - 2
    33 - 3
    41 - A

    if i send '95' i will get the hex value 2022
    for 96 i will get 2013.. the conversion problem occur in hex values from 80 to 9F...

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    As said above, char is coded in unicode, not in ASCII. While the lower part of unicode is identical to ASCII (00-7F, hex-wise), extended ASCII does not translate to unicode in the same way. That's why this happens.

  8. #8
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    Ok friend, then how can i convert the extended ASCII values to hex..
    give me step of help.. to write the program for that conversion

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rjagan View Post
    Ok friend, then how can i convert the extended ASCII values to hex..
    give me step of help.. to write the program for that conversion
    Don't bother about any conversion: as long as you only use the characters that are present on a US keyboard you can send them as is to your device, e.g.

    Java Code:
    sendToDevice('1'); // sends hexadecimal value 31
    sendToDevice('A'); // sends hexadecimal value 41
    ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. ASCII to EBCDIC conversion
    By satish kumar in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2013, 02:50 PM
  2. How to convert BCD to ascii
    By slavicp in forum Advanced Java
    Replies: 2
    Last Post: 04-05-2011, 12:54 PM
  3. Another ascii question....
    By akira220984 in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:38 AM
  4. I need help with ascii characters
    By Grandon in forum Eclipse
    Replies: 17
    Last Post: 11-08-2008, 03:12 AM
  5. Ascii code.........
    By Somitesh Chakraborty in forum New To Java
    Replies: 6
    Last Post: 11-04-2008, 06:32 PM

Posting Permissions

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