I'm trying to convert integers to char. Is there anyway to do this. I'm coding a game and am using rand to generate a range of numbers. After 10 these numbers have to be represented by letters. Thanks for your help.

Printable View

- 02-27-2002GaGiInt to Char
I'm trying to convert integers to char. Is there anyway to do this. I'm coding a game and am using rand to generate a range of numbers. After 10 these numbers have to be represented by letters. Thanks for your help.

- 02-27-2002adrianxw
Yes. Presumably you are converting the integers 0-9 to the characters '0' - '9' and want 10, for example to become 'A'? If so you can use the fact that characters are actually stored as integer codes so...

Code:`char x;`

int i;

i = 10;

x = (char) i + ('A' - 10);

- 02-27-2002ihsir
you could use itoa,ltoa etc functions in stdlib.h

- 02-27-2002adrianxw
>>> use itoa,ltoa

You have to be a bit careful here. Without knowing really what he is doing, (he didn't post his code!!!). itoa converts to a null terminated string, not a single character. - 02-27-2002ihsir
uhh... sorry

- 02-27-2002adrianxw
>>> uhh... sorry

Not necessary! That may well be what he wants, but without knowing what he is doing with his digits and letters, it is impossible to be sure!