# ANSI value of a single digit

• 05-24-2003
Iamien
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
• 05-24-2003
XSquared
int n = 5;
char c = n + '0';
• 05-24-2003
Iamien
erm thats does not work. if that was case then char 0 = 0 + 0 ...

i just used a function :D

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;     } }```
• 05-24-2003
XSquared
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.
• 05-24-2003
Magos
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... :D
• 05-24-2003
XSquared
Heh. When I clicked on the 'Post Reply' button, yours was there, and I thought it screwed up.
• 05-24-2003
Iamien
*hangs head in shame* thanks a lot hehe, saves a lot of overhead in the long run :D