-1

I'm using two ESp32 in a server and client configuration and i'm sending data between them in form of strings but i want to convert the recieved data which is fom of strings but it contains numbers into int on which i can workon to calculate the required things for my project. Can anyobne help me on how to convert string to integer?

3
  • String or char *...? – Majenko Apr 9 at 10:05
  • Have you tried googling that? I'm very sure, that this was explained many times throughout the internet – chrisl Apr 9 at 11:06
  • I don't know why atoi doesn't have a deprecation warning on it; please don't use atoi. If you think you're reaching for that use strtol instead. Unlike atoi, strtol has defined behaviour on overflow/underflow and can report that it failed and how, and can optionally tell you where the lex ended. – timemage Apr 9 at 12:34
1

The C library function atoi() will do that for you

0

The question was answered many times before, but you can convert an String to Integer either by converting it to an char array and using the atoi() function:

char strVal[10];
str.toCharArray(strVal, 10);
int val = atoi(strVal);

or simply using the toInt function:

int val = str.toInt();

Source: Arduino Forum

EDIT: Try to use the second solution as the atoi() function returns 0 on error, which can be misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.