# 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. ## 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. int n = 5;
char c = n + '0';

3. 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. 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.

5. 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...

6. Heh. When I clicked on the 'Post Reply' button, yours was there, and I thought it screwed up.

7. *hangs head in shame* thanks a lot hehe, saves a lot of overhead in the long run