Search code examples
arduinoesp32arduino-esp32

Substract first occurency from char*


I have this code on esp32 which get the return of command from an obd adapter

if (receive(buffer, sizeof(buffer)) > 0) {
    char *p = buffer;
    Serial.print("3 - p:");
    Serial.println(p);
    Serial.print("31 - size of p : ");
    Serial.println(strlen(p));
            
    p = strstr(p, "43 ");
    Serial.print("32 - p:");
    Serial.println(p);
 }
            

My goal is to substract the first 3 chars "43 " from the buffer And this is what I get in serial

3 - p:43 01 33 03 01 00 00 
>
31 - size of p : 23
32 - p:43 01 33 03 01 00 00 
>
33 - size of p : 23

How should I proceed to substract the first "43 " from the buffer ?

Thanks


Solution

  • If you just add an offset to the buffer pointer, so you're in effect skipping three characters, you should be fine:

    if (receive(buffer, sizeof(buffer)) > 0) {
        char *p = buffer+3;
        Serial.print("3 - p:");
        Serial.println(p);
    }
    

    This should print

    3 - p:01 33 03 01 00 00
    

    Is this what you want?