I'm trying to pass a parameter (that could be either an integer or a string) to a function . Since I haven't figured out how to accept both with the same variable in a function I am passing it as a char string/array and checking to see if it is a number at which point I convert it to an int and process. Example: I may type "ON" for the parameter or I may type "01" (the hex command for on for the hardware I am interfacing).
Two questions arise here:
-
why does "sizeof(chars)" always return 4?
-
when I decide that the char string is a number I convert it to the decimal equivalent of that ASCii character. However, it immediately changes back to ASCii. I have some guesses as to why this happens but don't really understand...
Here is the code:
void setup()
{
Serial.begin(115200);
printer("1234567");
}
void loop()
{
}
void printer(char chars[])
{
Serial.println(chars);
Serial.println(sizeof(chars));
Serial.println("\n\n");
if(chars[0] >= '0' && chars[0] <= '9')
{
for(int i = 0; i < (sizeof(chars) - 1); i++)
{
Serial.println(chars[i], DEC);
chars[i] -= '0';
Serial.println(chars[i], DEC);
Serial.println(chars[i], DEC);
Serial.println("\n\n");
}
}
Serial.println(chars[0], DEC);
Serial.println(chars);
}
And here is the serial monitor output:
1234567
4
49
1
49
50
2
50
51
3
51
49
1234567
Can someone shed some light on this? Thanks!