-2

My Arduino program receives a char* text message like this:

char* messageFromWebSocket = "4692\n4408\n656\n358\n662\n356\n658\n376\n656\n358\n662\n1394\n660\n1392\n636\n378\n632\n382\n638\n1418";

Now the IRremoteESP8266 library I used controls the IR LED through the sendRaw() function using this data, but in uint16_t form. Example:

uint16_t rawData[39] = {9064, 4408, 580, 4408,  580,  2152, 578, 2150,
                          580,  4408, 580, 30622, 9066, 2148, 580};
irsend.sendRaw(rawData, 39, 39);

Is there any way to convert my char* to uint16_t ?

I have:

char* messageFromWebSocket = "4692\n4408\n656\n358\n662\n356\n658\n376\n656\n358\n662\n1394\n660\n1392\n636\n378\n632\n382\n638\n1418";

I want get:

uint16_t* rawData = {4692, 4408,  656, 358,  662, 356,  658, 376,  656, 358,  662, 1394,  660, 1392,  636, 378,  632, 382,  638, 1418};

The size of the rawData will vary depending on the messageFromWebSocket, because each time the program passes, there is different data there. Thank you in advance for your help because I have no idea how to solve this.

misza
  • 13
  • 3
  • 1
    What have you tried? There are literally thousands of questions about integer to/from string conversions in C already on this site. Why can't you use `strtoul`? – Lundin Sep 23 '21 at 12:27
  • algorithm idea: parse the string, extract the number from `rawData[77]`, dynamically allocate memory for an array of this size, in a loop extract the numbers between braces and assign the values to the array. Check that the count of numbers between braces corresponds to the number between brackets. – Bodo Sep 23 '21 at 12:27
  • @Bodo This is for a microcontroller so dynamic memory allocation doesn't make any sense. Simply allocate enough memory to handle the worst-case scenario. No more, no less. – Lundin Sep 23 '21 at 12:29
  • 3
    Do you really receive this line of a C array definition, not just the values as text? This seems quite strange. – the busybee Sep 23 '21 at 12:37
  • @thebusybee Not really, the original text I get is: uint8_t *. I converted it to char * myself because I cut my first line from the message. I haven't written in C language for a long time. I mainly use high-level languages so I have a problem with simple conversion. – misza Sep 23 '21 at 13:44
  • This was not my question, my wording might be misleading. I meant, do you receive these characters 'u', 'i', 'n', 't', '1', '6', '_', 't', ' ', 'r', 'a', 'w', 'D', 'a', 't', 'a', and so on? – the busybee Sep 23 '21 at 14:31
  • @thebusybee Okay forgive me, I misunderstood. Yes I get exactly those characters, if I want to display messageFromWebSocket[0] I get 'u', messageFromWebSocket[1] = 'i' and so on. – misza Sep 23 '21 at 15:13
  • Well, then your program needs to get over these... (Why at all does the server send these?! Can't you change it?) However, Lundin's hint is a good way to follow. There is no way to compile a line of C in a running microcontroller, your parser-to-be-written is a kind of primitive interpreter. – the busybee Sep 23 '21 at 15:39
  • @thebusybee Ok that was a valuable comment, I changed the server message to send the value directly: char* messageFromWebSocket="4692,4408,656,358,662,356"; Could you please tell me how to replace it now with: uint16_t* rawData = {4692, 4408, 656, 358, 662, 356}; It would be best to see me some example. – misza Sep 23 '21 at 17:09
  • 1
    You should [edit] your question and add this important change. -- Then read the documentation of `strtoul()` and similar functions to learn how to use it. Depending on the document (your compiler's library manual?) there will be links to other functions and it will be in a chapter of converting functions. Experiment, it will not really help you if we post a readily usable "solution", especially since we don't know your development environment. Programming is learning, every day. – the busybee Sep 23 '21 at 18:54
  • As another hint, define an array of appropriate size, and fill its members with the values you received. -- If you stumble with a specific detail, feel free to [edit] your question again and add that. – the busybee Sep 23 '21 at 18:55

1 Answers1

0

below I show how I managed to do the conversion:

#define LENGTH 200
uint16_t rawData[LENGTH];

void parseCharToUint16WithIrSend(char* textData){
    Serial.printf("Parsing '%s':\n", textData);
    char *end;
    int sizeArray = 0;

    for (unsigned long i = strtoul(textData, &end, 10);
         textData != end;
         i = strtoul(textData, &end, 10))
    {
        textData = end;
        rawData[sizeArray] = i;
        Serial.printf("Value = %d\n", rawData[sizeArray]);
        sizeArray++;
    }
    
    printf("Actual array data size: %d\n", sizeArray);
    irsend.sendRaw(rawData,sizeArray,38); // here in rawData we have converted all values from char* to uint16_t
    Serial.println("=> DATA WAS SEND BY IR LED");
    
    sizeArray=0; // set index to 0
    memset(rawData, 0, LENGTH); // clear array
}

(I guess it may not be the best solution but it works)

misza
  • 13
  • 3
  • 1
    Repeatedly calling `strtoul` is a fine way to do it, I'd say. Given that the numbers are known to be separated by whitespace, there's nothing more to do. – Steve Summit Sep 23 '21 at 23:34