I want to convert Int type to its corresponding ASCII table number

-1
int i=2;
int caracterAscii=  ¿    ?  

should return the number 50.

How should I do it?

    
asked by Alber 17.04.2017 в 17:11
source

2 answers

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

    
answered by 17.04.2017 / 17:26
source
-2

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

    
answered by 17.04.2017 в 17:29