Skip to main content
added 238 characters in body; added 46 characters in body
Source Link
abelenky
  • 65k
  • 24
  • 115
  • 163

This will only work for int-digits 0-9, but your question seems to suggest that might be enough.

It works by adding the ASCII value of char '0' to the integer digit.

int i=6; char c = '0'+i; // now c is '6' 

For example:

'0'+0 = '0' '0'+1 = '1' '0'+2 = '2' '0'+3 = '3' 

Edit

It is unclear what you mean, "work for alphabets"? If you want the 5th letter of the alphabet:

int i=5; char c = 'A'-1 + i; // c is now 'F''E', the 5th letter. 

Note that because in C/Ascii, A is considered the 0th letter of the alphabet, I do a minus-1 to compensate for the normally understood meaning of 5th letter.

Adjust as appropriate for your specific situation.
(and test-test-test! any code you write)

This will only work for int-digits 0-9, but your question seems to suggest that might be enough.

It works by adding the ASCII value of char '0' to the integer digit.

int i=6; char c = '0'+i; // now c is '6' 

For example:

'0'+0 = '0' '0'+1 = '1' '0'+2 = '2' '0'+3 = '3' 

Edit

It is unclear what you mean, "work for alphabets"? If you want the 5th letter of the alphabet:

int i=5; char c = 'A' + i; // c is now 'F' 

This will only work for int-digits 0-9, but your question seems to suggest that might be enough.

It works by adding the ASCII value of char '0' to the integer digit.

int i=6; char c = '0'+i; // now c is '6' 

For example:

'0'+0 = '0' '0'+1 = '1' '0'+2 = '2' '0'+3 = '3' 

Edit

It is unclear what you mean, "work for alphabets"? If you want the 5th letter of the alphabet:

int i=5; char c = 'A'-1 + i; // c is now 'E', the 5th letter. 

Note that because in C/Ascii, A is considered the 0th letter of the alphabet, I do a minus-1 to compensate for the normally understood meaning of 5th letter.

Adjust as appropriate for your specific situation.
(and test-test-test! any code you write)

Source Link
abelenky
  • 65k
  • 24
  • 115
  • 163

This will only work for int-digits 0-9, but your question seems to suggest that might be enough.

It works by adding the ASCII value of char '0' to the integer digit.

int i=6; char c = '0'+i; // now c is '6' 

For example:

'0'+0 = '0' '0'+1 = '1' '0'+2 = '2' '0'+3 = '3' 

Edit

It is unclear what you mean, "work for alphabets"? If you want the 5th letter of the alphabet:

int i=5; char c = 'A' + i; // c is now 'F'