How to convert String to uint32_t?

How can I convert a String to a uint32_t correctly?

I’m trying to make a Particle function to set the calibration setting for a sensor and the calibration function needs a uint32_t argument.

Update:

Also, how can I properly convert uint32_t to a double to display as a cloud variable?

@nrobinson2000, take a look at atoi() and this:

https://docs.particle.io/guide/getting-started/hackathon/electron/#pass-strings

:slight_smile:

1 Like

If the number you're passing as a string might be larger than INT_MAX, you could use the C function strtoul() which converts c-strings to an unsigned long.

int functionHandler(String cmd) {
    uint32_t value = strtoul(cmd.c_str(), NULL, 10);
    Serial.printlnf("value is: %u", value);
    return 1;
}

If your numbers aren't that large, then you can either use atoi() with a c-string, or the String function toInt() and just cast the result to a uint32_t.

3 Likes

Thanks @Ric. I’m going to try this.