fork download
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4.  
  5.  
  6. // You don't need to copy these two functions in your arduino sketch
  7. // It's just a quick re-creation because they are not available here
  8.  
  9. char * itoa(int val, char * s, int radix)
  10. {
  11. sprintf(s, "%d", val);
  12. return s;
  13. }
  14.  
  15. char * dtostrf(double val, signed char width, unsigned char prec, char * sout)
  16. {
  17. sprintf(sout, "%*.*f", width, prec, val);
  18. return sout;
  19. }
  20.  
  21.  
  22.  
  23. int main()
  24. {
  25. int IDX = 123;
  26. bool VALUE = true;
  27. float VOLTAGE = 1.2345f;
  28.  
  29. char str1[64];
  30. snprintf(str1, sizeof(str1), "idx=%d&value=%s&voltage=%.2f", IDX, VALUE ? "true" : "false", VOLTAGE);
  31. printf("str1 = <%s>\n", str1);
  32.  
  33. char str2[64];
  34. char tmp[16];
  35. strcpy(str2, "idx=");
  36. strcat(str2, itoa(IDX, tmp, 10));
  37. strcat(str2, "&value=");
  38. strcat(str2, VALUE ? "true" : "false");
  39. strcat(str2, "&voltage=");
  40. strcat(str2, dtostrf(VOLTAGE, 0, 2, tmp));
  41. printf("str2 = <%s>\n", str2);
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 4632KB
stdin
Standard input is empty
stdout
str1 = <idx=123&value=true&voltage=1.23>
str2 = <idx=123&value=true&voltage=1.23>