I want to convert Stringto int, and all I could find is that you need to convert the String array to a char array, and then apply that array to int, but my code produces strange values and I can't figure out what the problem is.
void ledDimm(String command)
{
String substring = command.substring(8, command.length());
Serial.println("SubString:");
Serial.println(substring);
Serial.println("SubString Length:");
Serial.println(substring.length());
char valueArray[substring.length() + 1];
Serial.println("sizeof ValueArray");
Serial.println(sizeof(valueArray));
substring.toCharArray(valueArray, sizeof(valueArray));
Serial.println("valueArray:");
Serial.println(valueArray);
int value = int(valueArray);
Serial.println("Integer Value:");
Serial.println(value);
analogWrite(LEDPin, value);
}
And the serial output is as follows:
Received packet of size 11
From 192.168.1.4, port 58615
Contents:
LEDDimm=100
SubString:
100
SubString Length:
3
sizeof ValueArray
4
valueArray:
100
Integer Value:
2225
I was expecting to get an int with a value of 100, but the actual int is 2225? What have I done wrong here?
source
share