been searching everywhere, but couldn’t the correct answer.
the problem is pretty simple:
i have to convert ASCII integer values into char’s.
for example, according to ASCII table, 108 stands for ‘h’ char. But when i try to convert it like this:
int i = 108
char x = i
and when I printf it, it shows me ‘s’, no matter what number i type in(94,111…).
i tried this as well:
int i = 108;
char x = i + '0'
but i get the same problem! by the way, i have no problem in converting chars into integers, so i don’t get where’s the problem :/
thanks in advance
That is how you do it. You probably want it unsigned, though.
Maybe your
printfis wrong?The following is an example of it working:
This prints
abcdefghijklmnopqrstuvwxyzas expected. (See it at ideone)Note, you could just as well
printf("%c", i);directly;charis simply a smaller integer type.If you’re trying to do
printf("%s", x);, note that this is not correct.%smeans print as string, however a character is not a string.If you do this, it’ll treat the value of
xas a memory address and start reading a string from there until it hits a\0. If this merely resulted in printings, you’re lucky. You’re more likely to end up getting a segmentation fault doing this, as you’ll end up accessing some memory that is most likely not yours. (And almost surely not what you want.)