Resume some programming lines

Hi all,

French beginner question : how to sumarize the below lines ? I don't know how to concatenate a variable and a variable... I try to be clear, but just have a look below and you will understand :

ind1=readString.indexOf("DT1");
ind2=readString.indexOf("DT2");
ind3=readString.indexOf("DT3");
ind4=readString.indexOf("DT4");
ind5=readString.indexOf("DT5");
ind6=readString.indexOf("DT6");
ind7=readString.indexOf("DT7");
mac[0] = readString.substring(ind1+4, ind2-1);
mac[1] = readString.substring(ind2+4, ind3-1);
mac[2] = readString.substring(ind3+4, ind4-1);
mac[3] = readString.substring(ind4+4, ind5-1);
mac[4] = readString.substring(ind5+4, ind6-1);
mac[5] = readString.substring(ind6+4, ind7-1);

I have to parse a http request and work with it. Thiks is an extract of what I got to do with this request. I think that I will load the Arduino memory with dummy bites... this is basic programming, but didn't found any potential answer on the web...
Thanks, thanks thanks

Please post an example of the line you are trying to parse - we could surely find a better way of doing it, if only we knew what "it" is.

This is the request. In fact, I've used some codes of another project for updating the mac, ip, subnet dans gw of my arduino Ethernet. Problem, the paste/append is not always a miracle. Need to modify the code for my project. I cannot use the TextFinder library for misc reasons.

Then, the http request is created by a form generated by the Arduino.
http://192.168.1.200/setup?SBM=1&DT1=DE&DT2=AD&DT3=BE&DT4=EF&DT5=FE&DT6=ED&DT1=222&DT2=173&DT3=190&DT4=239&DT5=254&DT6=237&DT7=192&DT8=168&DT9=1&DT10=200&DT11=255&DT12=255&DT13=255&DT14=0&DT15=192&DT16=168&DT17=1&DT18=1

from DT1 to DT6 : mac address
DT7 to DT10 : ip
and so on

Personally I would iterate through the string looking for certain tokens and slice the string up as I go.

strtok() might do the job (it's a tricky function to get right), or manual iteration.

For manual iteration I'd have a couple of pointers, one for the current position, one for the start of a "key", and one for the start of a "value".

Start at the beginning of the string, and set the key pointer to that position. Iterate along until you hit an =. Set it to the null character, and set the value pointer to the next character along. Keep going until you hit an & or the end of the line, then set it to null. You now have two strings, one with the key, and one with the data. So what you want with those strings, then start again from the next character in the string.

Something like:

void setup()
{
  char *string = "SBM=1&DT1=DE&DT2=AD&DT3=BE&DT4=EF&DT5=FE&DT6=ED&DT1=222&DT2=173&DT3=190&DT4=239&DT5=254&DT6=237&DT7=192&DT8=168&DT9=1&DT10=200&DT11=255&DT12=255&DT13=255&DT14=0&DT15=192&DT16=168&DT17=1&DT18=1";
  char *pos,*key,*data;
  Serial.begin(9600);
  
  key = pos = string;
  while(*pos)
  {
    switch(*pos)
    {
      case '=':
        *pos = 0;
        pos++;
        data = pos;
        break;
      case '&':
        *pos = 0;
        Serial.print("Key: ");
        Serial.print(key);
        Serial.print(" Value: ");
        Serial.println(data);
        pos++;
        key = pos;
        break;
    }
    pos++;
  }
  Serial.print("Key: ");
  Serial.print(key);
  Serial.print(" Value: ");
  Serial.println(data);
}

void loop()
{
}

It doesn't matter with that code what order the parameters are in, or what size the numbers are. And it's very very memory efficient - everything stays in the memory it is already in, you just modify it "in place".

Hi,

I found this library really helpful for matching patterns in HTML responses:
http://www.arduino.cc/playground/Code/TextFinder

HTH
paul

Paul_Martinsen:
Hi,

I found this library really helpful for matching patterns in HTML responses:
http://www.arduino.cc/playground/Code/TextFinder

HTH
paul

You might like to re-read the original post...

I cannot use the TextFinder library for misc reasons.

hmm. sorry. missed that.

Would one reason be crashing due to memory overflow?

TextFinder can generate problems as string is volatile and is lost when read. In my case, I cannot use this library, even if I've used it before and can be really usefull in some cases.

%Thanks a lot for your lines, Majenko. You saved me on this.

The overall project : Catch a RFID signal using Wiegand Reader, send a http to a server to store the signal, and server answers on an LCD. All of this connected on an Ethernet Arduino. This is for a University to control presence of students in specific courses.

All is running but only this part was making me crazy (killed the Arduino memory)

All is running but only this part was making me crazy (killed the Arduino memory)

C++ Strings can do that easily. To change the String it makes a new string, deletes the old and your heap space gets fragmented. An UNO has 2k ram for heap and stack. Sure you can use Strings but take care how much and how hard.

Alternative is to use C character array strings (as opposed to Strings) that do not misbehave so.

majenko,

Played a bit with your solution. Perfect for me, but when integrating in the final sketch, I have an issue while trying to associate a string to a *char.
Once again, surfed on the web and no working solution to solve this new issue.

Problem, I must keep the http in a string format for other treatment (to use the string library) as I am receiving multi htpp in misc format.
How can I convert the http string in *char ?

Thanks once again

string.toCharArray(buffer,len).

char buffer[80];
String str;

str = "This is my string";

str.toCharArray(buffer,80);