Hi !
I there a better way to load an array then this :
nodetemp[0] = buffer[0];
nodetemp[1] = buffer[1];
nodetemp[2] = buffer[2];
temperature = atoi(nodetemp);
maxtemp[0] = buffer[3];
maxtemp[1] = buffer[4];
maxtemp[2] = buffer[5];
maxtemperature = atoi(maxtemp);
mintemp[0] = buffer[6];
mintemp[1] = buffer[7];
mintemp[2] = buffer[8];
mintemperature = atoi(mintemp);
nodehum[0] = buffer[9];
nodehum[1] = buffer[10];
nodehum[2] = buffer[11];
humidity = atoi(nodehum);
maxhum[0] = buffer[12];
maxhum[1] = buffer[13];
maxhum[2] = buffer[14];
maxhumidity = atoi(maxhum);
minhum[0] = buffer[15];
minhum[1] = buffer[16];
minhum[2] = buffer[17];
minhumidity = atoi(minhum);
Thanks Allen C.
much easier would be to work backwards:
#define MINHUM_IDX 15
#define MAXHUM_IDX 12
#define HUMIDITY_IDX 9
#define MINTEMP 6
#define MINTEMP 3
#define NODTEMP 0
int minhumidity, maxhumidity, humidity,
mintemperature, maxtemperature, temperature;
char buffer[20];
void bar(void)
{
buffer[18] = 0; // assure a terminating null
minhumidity = atoi(buffer+ MINHUM_IDX);
buffer[MINHUM_IDX] = 0; // step on processed field to place null terminator
maxhumidity = atoi(buffer+ MAXHUM_IDX);
buffer[MAXHUM_IDX] = 0; // step on processed field to place null terminator
humidity = atoi(buffer+ HUMIDITY_IDX);
buffer[HUMIDITY_IDX] = 0; // step on processed field to place null terminator
mintemperature = atoi(buffer+ MINTEMP_IDX);
buffer[MINTEMP_IDX] = 0; // step on processed field to place null terminator
maxtemperature= atoi(buffer+ MAXTEMP_IDX);
buffer[MAXTEMP_IDX] = 0; // step on processed field to place null terminator
temperature= atoi(buffer+ NODTEMP_IDX);
}
or as a loop+table :
char *fields[] = { buffer + MINHUM_IDX,
buffer + MAXHUM_IDX,
buffer + HUMIDITY_IDX,
buffer + MINTEMP_IDX,
buffer + MAXTEMP_IDX,
buffer + NODTEMP_IDX
};
int *destination[] = { &minhumidity,
&maxhumidity,
&humidity,
&mintemperature,
&maxtemperature,
&temperature
};
void foo(void)
{
buffer[18] = 0; // assure a terminating null
for (i = 0; i < 6; ++i) // loop through all six fields
{
*destination[i] = atoi(fields[i]); // get it
fields[i][0] = 0; // set up for the next atoi()
}
}
Sorry about not getting the response formatted better - I’m having a senior moment and forget how 