Results 1 to 5 of 5

Thread: ASCII value for char in java

  1. #1
    Join Date
    Jun 2006
    Location
    Syracuse, NY
    Beans
    3,264

    ASCII value for char in java

    Is there a method in java to return the ASCII value of a char as an int? I can't find one in the API, but I think it's just cause I'm blind
    Desktop: AMD Athlon64 X2 3600+, Nvidia 8600GT, 3GB RAM, 80GB hd, Windows 7 Beta
    Lappy: Sony Vaio FW-140E, Intel P8400 2.26Ghz, 3GB Ram, 250GB HD, Intel x4500MHD, Windows 7 Beta & Kubuntu 8.10 w/ KDE 4.2

  2. #2
    Join Date
    Oct 2006
    Location
    Minnesota, USA
    Beans
    227
    Distro
    Ubuntu 6.06

    Re: ASCII value for char in java

    Quote Originally Posted by igknighted View Post
    Is there a method in java to return the ASCII value of a char as an int?
    Code:
    char somechar = ((char)in.readLine()); 
    int i = Integer.parseInt(somechar); 
    System.out.println(i);
    Hello,
    Above takes a keyboard char (ASCII, I believe) and the second line does what you want.
    HTH,
    Dave

  3. #3
    Join Date
    Jun 2006
    Location
    Syracuse, NY
    Beans
    3,264

    Re: ASCII value for char in java

    Quote Originally Posted by DaveBorealis View Post
    Code:
    char somechar = ((char)in.readLine()); 
    int i = Integer.parseInt(somechar); 
    System.out.println(i);
    Hello,
    Above takes a keyboard char (ASCII, I believe) and the second line does what you want.
    HTH,
    Dave
    I'm getting a syntax error saying
    Code:
    Cannot find symbol
    symbol : method parseInt(char)
    location : class java.lang.Integer
    Desktop: AMD Athlon64 X2 3600+, Nvidia 8600GT, 3GB RAM, 80GB hd, Windows 7 Beta
    Lappy: Sony Vaio FW-140E, Intel P8400 2.26Ghz, 3GB Ram, 250GB HD, Intel x4500MHD, Windows 7 Beta & Kubuntu 8.10 w/ KDE 4.2

  4. #4
    Join Date
    Jun 2006
    Location
    Syracuse, NY
    Beans
    3,264

    Re: ASCII value for char in java

    Ok, sorry to double post, but I found the answer. All I needed was to do an explicit conversion:
    Code:
    charact = sentence.charAt(i);
    ascii = (int)charact;
    Thanks for the suggestion
    Desktop: AMD Athlon64 X2 3600+, Nvidia 8600GT, 3GB RAM, 80GB hd, Windows 7 Beta
    Lappy: Sony Vaio FW-140E, Intel P8400 2.26Ghz, 3GB Ram, 250GB HD, Intel x4500MHD, Windows 7 Beta & Kubuntu 8.10 w/ KDE 4.2

  5. #5
    Join Date
    Oct 2006
    Location
    Minnesota, USA
    Beans
    227
    Distro
    Ubuntu 6.06

    Re: ASCII value for char in java

    Quote Originally Posted by igknighted View Post
    I'm getting a syntax error saying
    Code:
    Cannot find symbol
    symbol : method parseInt(char)
    location : class java.lang.Integer
    My mistake. parseInt takes a string, not a char!

    There's also java.lang.getNumericValue(char ch), if you want to keep playing around, but I just looked it up and it seems to be unicode based rather than ASCII.

    Glad you got it to work!
    Dave

Bookmarks

Posting Permissions

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