In my current program, I need to format an integer into an ASCII character. This will work with sprint() or snprintf().
Is there any alternative to sprintf() or snprintf() to format the data?
Thanks, Pavan.
In my current program, I need to format an integer into an ASCII character. This will work with sprint() or snprintf().
Is there any alternative to sprintf() or snprintf() to format the data?
Thanks, Pavan.
char ch = (char)(i + (int)'0'); /* Where i is an integer */
This works for 0 to 9
You could use any of *printf()
functions, such as printf, s(n)printf, fprintf
etc. *print**f**
means "formatted printing". You might also try printf("%c",(char)65)
but this might not help you very much.
Built-in functions are in most cases faster than "self-written".
The original question didn't make it clear that the input is a string containing an integer in decimal form, but reading the comments leads to a much lighter weight solution than sprintf
(char)(atoi(decimal_str))
Casting to char
ensures the integer will be limited to 8 bits with the code of the same value. atoi
converts a string of digits into an integer