Results 1 to 12 of 12
  1. #1
    Ruddlesdin is offline Member
    Join Date
    Nov 2013
    Location
    Cambridge
    Posts
    5
    Rep Power
    0

    Default Decimal to ASCII

    Hi all,
    I have been writing a class method that converts a decimal value into ASCII but I am really struggling with it.

    public String counterValue(int value)

    so if I input 52 then the output should be '4'
    if I input 48 then the output should be '0'
    if I input 50 then the output should be '2'

    OK, so far I have managed to do this but:
    if I input 152 then I am expecting '' but instead I see a '~' and I can't understand why.
    I used (char)152 which I expected to return
    Can anybody help?

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Decimal to ASCII

    Java strings and characters use UTF-8, not ASCII bytes. And ASCII only goes up to 127. "Extended ASCII" was never standardized, and the glyphs for bytes 128-255 varied between platforms. The Unicode characters corresponding to the glyphs of the "extended ASCII" most people are familiar with, IBM codepage 437, are not UTF-8 characters 128-255. In fact, they're not even contiguous. If you want to use those glyphs in Java strings, use the values in the table here: Code page 437 - Wikipedia, the free encyclopedia
    Get in the habit of using standard Java naming conventions!

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Decimal to ASCII

    You're in luck, I just finished one of these. The first thing I did was split the input string up into individual words via the split method. Then iterate over each word in the string and convert it to text (ASCII) using a method in the Integer class.
    Java Code:
    int decimal = Integer.parseInt("Hello", 10); //10 is the base in which you are converting from (decimal is base 10)
    You want to add this to an ArrayList<Character>
    Java Code:
    chars.add((char) decimal); //casting char to an int converts the integer to a character
    The last thing you need to do is iterate over that ArrayList<Character> and add each char to a string using the String.concat(String s) method.

    I did some research for you. ASCII is 7-bit, meaning that there are 128 different printable characters. Extended ASCII was made to allow 128 more characters, in use for other languages. These are not standard ASCII characters, therefor not convertible (unless you make an extended ASCII converter).
    Last edited by zFollette; 12-30-2013 at 05:11 AM.

  4. #4
    Ruddlesdin is offline Member
    Join Date
    Nov 2013
    Location
    Cambridge
    Posts
    5
    Rep Power
    0

    Default Re: Decimal to ASCII

    Thanks for the replies kjkrum and zFollette. I now understand what you mean about the extended ASCII. I will try later to use the UTF-8 encoding instead of US-ASCII encoding. Thanks

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Decimal to ASCII

    Quote Originally Posted by zFollette View Post
    Java Code:
    int decimal = Integer.parseInt("Hello", 10); //10 is the base in which you are converting from (decimal is base 10)
    What is that? Why are you showing an example that would generate an exception (which you didn't mention). The string "Hello" is not a legal value in base 10.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Decimal to ASCII

    Java Code:
    int decimal = Integer.parseInt("Hello", 10);
    Umm... what? That would just throw a NumberFormatException.

    OP: here's a class that converts ASCII bytes to and from the UTF-8 characters that correspond to the glyphs of codepage 437. It encodes characters to ints instead of bytes because the library it's part of encodes terminal buffers in an int[][]. https://github.com/kjkrum/JTX/blob/m...jtx/Cp437.java
    Get in the habit of using standard Java naming conventions!

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Decimal to ASCII

    My bad, I just wanted to show an example of a string, forgot I was converting numbers. My fault.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Decimal to ASCII

    Well, if you change 10 to 25 (or higher) it would convert fine. But of course the decimal equivalent would change for each base.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Decimal to ASCII

    Quote Originally Posted by jim829 View Post
    Well, if you change 10 to 25 (or higher) it would convert fine. But of course the decimal equivalent would change for each base.

    Regards,
    Jim
    I will test this.

    EDIT: If I read your post right, you are saying to convert 152 into base 25, then converting that base 25 number into ASCII. Right?

    I am laughing right now at all of our mistakes. The tilde is also on the extended ASCII table as 152 (according to ascii-code.com) and the character you want is actually 159, or 255 depending on upper or lower case.
    Last edited by zFollette; 12-31-2013 at 05:51 AM.

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

    Default Re: Decimal to ASCII

    Quote Originally Posted by zFollette View Post
    I am laughing right now at all of our mistakes. The tilde is also on the extended ASCII table as 152 (according to ascii-code.com) and the character you want is actually 159, or 255 depending on upper or lower case.
    Make that your mistakes; the ASCII (and Unicode) value for the tilde (~) is 0x7E and is in the standard ASCII table.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Decimal to ASCII

    Quote Originally Posted by zFollette View Post
    I will test this.

    EDIT: If I read your post right, you are saying to convert 152 into base 25, then converting that base 25 number into ASCII. Right?

    All I was talking about was that you can convert "Hello" to a decimal number by specifying a base of 25 or higher. Base 10 uses the symbols 0-9. Base 16 adds a-f and base 25 would add an additional g-o. So from the digit zero to the letter "o" you have 25 symbols.

    Regards,
    Jim
    Last edited by jim829; 12-31-2013 at 04:12 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Decimal to ASCII

    yes, yes you can. I thought we were past my lapse of judgment. Here's your conversion: 2M 41 48 48 4B

Similar Threads

  1. Replies: 1
    Last Post: 11-19-2013, 11:25 PM
  2. Using ASCII...
    By putrify in forum New To Java
    Replies: 4
    Last Post: 09-10-2011, 06:17 AM
  3. Replies: 5
    Last Post: 08-25-2011, 08:32 PM
  4. help - Ascii to hex and hex to ascii conversion
    By rjagan in forum New To Java
    Replies: 8
    Last Post: 06-14-2011, 01:53 PM
  5. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 10:59 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
  •