
Patrick B. answered 05/07/19
Math and computer tutor/teacher
Your question is how to convert an integer into a string.
One way is to use sprintf as seen in the code snippet below.
However, itoa does work just fine, and converts to binary and hex.
Make sure you are including the proper header files. You need
stdlib.h and string.h.
Otherwise , there may be a compiler glitch as to why those headers
cannot be found, pre-processed, compiled, and linked. Usually, it
is a directory issue... the compiler is not looking into the proper folder
for the headers, OR the headers are not there for whatever reason.
THe following code snippet works correctly in Bloodshed IDE:
==========================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a=100;
char buffer[20];
itoa(a,buffer,2); // here 2 means binary
printf("Binary value = %s\n", buffer);
itoa(a,buffer,10); // here 10 means decimal
printf("Decimal value = %s\n", buffer);
itoa(a,buffer,16); // here 16 means Hexadecimal
printf("Hexadecimal value = %s\n", buffer);
//writes a to the buffer
sprintf(buffer,"%d",a);
printf(">%s<",buffer);
return 0;
}