Somebody asked how to convert an integer to a string. ( int A=123 to a String V='123').
The normal answer seems to be "Use Serial.println()".
Please can somebody tell, how using Serial.print helps, if you do not want the value printed to serial port, but wan to have a string, which can be handled further .
This is the simple (corrected typo) command, I have been using.
Never understood, why some recommend this Serial.print().
Normally I newer use one character variable names nor uppercase, but here they were just for example.
Can you please provide one or more references where it's suggested to use Serial.println()?
I suspect that it all depends on the use case where you see it. The most common case that I'm aware of is where a user wants to print an integer, first converts it to String (which probably fails and hence a question); the advice in that case is why to convert in the first place and not just use Serial.print().
but how does sprintf() or the underlying functions do it
i imagine something like
void
convert (
int val )
{
int num = val;
int dec = 0;
int base = 1;
printf ("%s: %6d\n", __func__, val);
for ( ; val; val /= 10) {
int dig = val % 10;
dec = (dec << 4) + dig;
// dec += base * dig;
base = base * 16;
printf (" %6d", val);
printf (" %6d", dig);
printf (" %6x", dec);
printf (" %6d", base);
printf ("\n");
}
char s [10];
memset (s, 0, sizeof(s));
for (int i = 0; dec; dec >>= 4, i++) {
s [i] = '0'+ (dec & 0xF);
printf (" %6d - '%6s' %d - %x\n", num, s, i, dec & 0xf);
}
}