I need help. I am having trouble converting an int into a char string. Is there a special command for this?
Thanks,
sprintf is one way.
I have not found out how to use sprintf and I don't know where the library for it is. Help!
I find Google handy in such situations, and I don't mean that sarcastically.
Anyway I found this:
Here's a start:
int aInt = 2468; // Test number
char asciiGroupOfNumbers[7]; // Max -32768, which is 6 chars + null
int tensComponent;
byte digitCounter;
digitCounter = 0;
if (aInt < 0)
{
asciiGroupOfNumbers[0] = '-';
aInt = -aInt;
++digitCounter;
}
for (tensComponent = 10000; tensComponent; tensComponent /= 10)
{
asciiGroupOfNumbers[digitCounter++] = aInt / tensComponent + '0';
aInt %= tensComponent;
}
asciiGroupOfNumbers[digitCounter] = 0; // Add the null terminator
You might want to deal with leading 0 suppression, and maybe deal with the -32768, but other than that I think it works.
luketheduke:
I need help. I am having trouble converting an int into a char string. Is there a special command for this?
Thanks,
You don't show the code context in which you are working, such as c-strings or Strings. In the past somebody posted the below as examples that might be of use.
//convert a integer into a string
val6 = year();
String s1 = String(val6);
int number;
char msg[20];
sprintf(msg,"%d",number);