int i=2;
int caracterAscii= ¿ ?
should return the number 50.
How should I do it?
int i=2;
int caracterAscii= ¿ ?
should return the number 50.
How should I do it?
int i=2;
You already start badly. If you want to convert a character to a whole you have to use a character :
char i='2';
Ok, now how does it become whole? You should know that the type char
is a 1 byte integer with a sign, then the conversion is instantaneous:
char i='2';
int numero = (int)i;
printf("%d %d",numero,i);
In fact you can see in the previous example that you would not need to pass it to int
.
Why does it work?
As I have said, the type char
is not more than an integer of 8 bytes, that is, a number. What happens is that when printing the operating system does not print the number directly, but it goes to a translation table where it extracts the corresponding ASCII character.
48 + i
Functional for 0 to 9. If you have a number greater than 9, two digits on digits could use some function like it to pass the int number, a buffer for the string and the base (usually 10).