Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Converting a string to a char array without using extra memory

Status
Not open for further replies.
If you are going to use the String class, the reason would be that you want to use the operators and functions that the class offers. Otherwise, just stay with a string (i.e., a c string).
The problem is that many libraries return strings and sometimes you need to treat then as C strings so you have no choice in the matter.

Mike.
 
If your data is simple then you can convert it to json manually. Here's how I send weight, temperature and humidity when requested. It might save some room by not needing the json library.

Mike.
Code:
void handleWeight(){
  strcpy(buff,"{");
  sprintf(temp,"\"weight\":\"%.3f\"",scale.get_units());
  strcat(buff,temp);
  sprintf(temp,",\"DHTtemp\":\"%d\"",temp10);
  strcat(buff,temp);
  sprintf(temp,",\"DHThumid\":\"%d\"}",humid10);    //note closing brace
  strcat(buff,temp);
  server.send(200,"application/json",buff);  
}
 
I've used sprintf before kinda handy, and I did a json reply with it, but I didnt use strcat(), is that similar to concat().
I have 2 json replys, one for current settings, and another for recorded values to be displayed on a chart, the latter is the trouble.
I might be able to put something together using sprinf to output the char array in a for loop.
I'm dealing with melt temps, melt pressures & drive current, but essentially the same thing.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top