ANSI value of a single digit

This is a discussion on ANSI value of a single digit within the C++ Programming forums, part of the General Programming Boards category; Is there a way to get the ANSI value of a digi? IE 0 = 48 1 = 49 and ...

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    ANSI value of a single digit

    Is there a way to get the ANSI value of a digi?
    IE 0 = 48 1 = 49 and so forth.
    I'm currently just coding my own with a bunch of If statements but would be nice to know of a pre built

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    int n = 5;
    char c = n + '0';
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    erm thats does not work. if that was case then char 0 = 0 + 0 ...

    i just used a function

    Code:
    int ansival(int x) {
      if (x==0) {
        return 48;
        }
      if (x==1} {
        return 49;
        }
      if (x==2) {
        return 50;
        }
      if (x==3) {
        return 51;
        }
      if (x==4) {
        return 52;
        }
      if (x==5) {
        return 53;
        }
      if (x==6) {
        return 54;
        }
      if (x==7) {
        return 55;
        }
      if (x==8) {
        return 56;
        }
      if (x==9) {
        return 57;
        }
    }

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You forgot the single quotes around the 0. '0' returns the ASCII value of 0. So n + '0' is the ASCII value of a number.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You didn't read it close enough. He said '0' not 0. '0' is the character 0, not the number 0. If you convert '0' to an integer you get 48. The formula he gave you is correct.

    EDIT: Touche, beaten by 0 minutes. If I just pressed the "post reply" button instead of taking an extra breath...
    Last edited by Magos; 05-24-2003 at 05:25 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Heh. When I clicked on the 'Post Reply' button, yours was there, and I thought it screwed up.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    *hangs head in shame* thanks a lot hehe, saves a lot of overhead in the long run

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  2. Verifying single digit input
    By Syked4 in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 07:11 PM
  3. leading zero on single digit int
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21