0

I have a string:

String cod=server.arg("codice");

where codice (an IR sequence) is retrieved from a webserver. For example:

codice=1, 2, 3, 4

In which the syntax can be modified.

So, cod is a string that contains numbers separated with ", "

To send the IR code i use:

irsend.sendRaw(rawData, 67, 38);

I don't know how to convert the String to Int (rawData) as requested by rsend.sendRaw.

  • 1
    Your question has nothing to do with Arduino. – user31481 Sep 23 '17 at 12:56
  • @darimos on Stack Exchange sites, solutions are not to be edited into questions. Rather, accept an answer which provides the solution, or if none does, post your own answer and return to accept that once the few-days waiting period of self answers has elapsed. – Chris Stratton Sep 23 '17 at 18:12
1

Use strtok to split the char array into the different parts, then use atoi on each of the parts to convert it to an int. (atoi ignores whitespace)

char* str = "4532, 4488,  548, 1694,  574";

const size_t bufferSize = 5;
int arr[bufferSize];

char *p = strtok(str, ",");
size_t index = 0;

while (p != nullptr && index < bufferSize) {
  arr[index++] = atoi(p);
  p = strtok(NULL, ",");
}

for (size_t i = 0; i < index; i++)
  Serial.println(arr[i]);

Edit: You just edited your post, and it seems like you really need to use Strings (capital S). There are two ways to go about it: you have to keep in mind that String::c_str() returns the actual pointer to the internal null terminated char array inside the String object, and that strtok is destructive (it replaces the delimiters with null characters).
If you need the String for other things after converting it to an array of ints, you'll have to make a copy first:

String cod = "4532, 4488,  548, 1694,  574";
char *str = new char[cod.length() + 1];
strcpy(str, cod.c_str());

Don't forget to free the memory afterwards:

delete[] str;

If you don't care about destroying the String, you can just use:

String cod = "4532, 4488,  548, 1694,  574";
char *str = cod.c_str();

Edit 2: The ESP8266 compiler doesn't allow implicit conversion from const char* (read only) to char* (as strtok argument). To get around it, the clean solution is to copy it, as explained in my first edit.
If you really don't need the String afterwards, and you don't want to waste memory on the copy, you can just use an explicit type cast:

String cod = "4532, 4488,  548, 1694,  574";
char *str = (char*)cod.c_str();

The AVR compiler doesn't have any problems converting String::c_str() to char*

  • Hello, thanks for the answer. I tried your solution but i got this error: char _EXFUN(strtok,(char *, const char *)); ^ exit status 1 invalid conversion from 'const char' to 'char*' [-fpermissive] – darimos Sep 23 '17 at 13:34
  • Do you mean for (size_t i = 0; i < index; i++) { Serial.print(arr[i]); Serial.print('\t'); } Serial.println(); ? – tttapa Sep 23 '17 at 14:14
  • Since i need to send irsend.sendRaw(rawData, 67, 38); i need an int array called rawData or whatelse is the name. I hope it is clear, thank you. – darimos Sep 23 '17 at 14:21
  • arr is the array of ints – tttapa Sep 23 '17 at 14:25
  • yes, i tried to do Serial.println(arr); but i received "call of overloaded 'println(int [5])' is ambiguous" which is the problem? – darimos Sep 23 '17 at 14:27

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.