-3

I am currently doing a college project and most of my background comes from JS & Python. I have an Arduino that is receiving data and using ArduinoJSON to take in settings and deserialize them. I have it set up that the setting is a const char* setting. Which can contain a range of data types (mostly int or bool). But I am focused on trying to get a bool value from the char.

setting = doc["setting"];
if (setting == "true") {
  test = true;
} else if (setting == "false") {
  test = false;
}

I tried the above with no luck. I also tried converting the char to a string but with no luck. I also tried to use the .compare() function with no luck either. What would you guys recommend?

IAmAndy
  • 121
  • 2
  • 12

1 Answers1

1

With const char*, the == operator will compare the pointer values rather than the text value (which will generally always be different since the data is stored in different locations). You could change it to:

if (strcmp(setting, "true") == 0) { test = true; }
else if (strcmp(setting, "false") == 0) { test = false; }

instead to get things working. Alternatively you could cast the setting to a String before doing the comparison and that should work as well:

String setting = String(doc["setting"]);
if (setting == "true") { test = true; }
else if (setting == "false") == 0) { test = false; }
mattlangford
  • 1,260
  • 1
  • 8
  • 12