how do I get the String class working? My IDE is 0.0.12 (Arduino 0018).
I know that Arduino 0019 has the string class,but the latest maple IDE doesnt include it.
What do I need to do to get String to work?
how do I get the String class working? My IDE is 0.0.12 (Arduino 0018).
I know that Arduino 0019 has the string class,but the latest maple IDE doesnt include it.
What do I need to do to get String to work?
More specifically, I need to retrieve an integer from a string. I'm working on a web server interface that sends the value of a number field in a string like this:
"GET /ajax_inputs&PWM3=1000&nocache=120749.0530796349"
I need to extract the "1000" from "PWM3=1000" in the string, and use that value to write to a PWM pin.
The string library in Arduino (or the standard C string library for that matter) can do it by the substring method, but is there a different way I can do it without the bloat of the string library?
This should be straight-forward with the C functions 'strstr' and 'atoi'.
extern "C" {
#include <string.h>
#include <stdlib.h>
char *req = "GET /ajax_inputs&PWM3=1000&nocache=120749.0530796349";
char *ptr = strstr(req, "PWM3=");
if(ptr)
{
int pwn = atoi(ptr+5);
// use pwn
}
}
Or using sscanf...
extern "C" {
#include <stdio.h>
int pwn;
sscanf(req, "GET /ajax_inputs&PWM3=%i", &pwn);
printf("%i\n", pwn);
}
ventosus - thank you for your answers. I tried the first example on Arduino and it works well.
I then tried it on maple but I get 'atoi' not declared. How do I add 'atoi'? I found this from the FAQ:
"How can I use atoi()?
The CodeSourcery GCC compiler used to compile your programs is configured to link against the newlib C library, and allows the use of any of its headers."
This doesn't tell me much...nor how to make it compile in my program. Is there another step I need to use 'atoi'?
Also, I dont have 'String.h'. Where does this library come from?
Also If I try to include <stdlib.h> I get:
\maple-ide-0.0.12-windowsxp32\hardware\tools\arm\bin\../lib/gcc/arm-none-eabi/4.4.1/../../../../arm-none-eabi/include/stdlib.h:72
stdlib.h:72: error: expected unqualified-id before 'int'
I was able to get <stdlib> to work, I had to add it at the top of my includes
<stdlib>
...
<other includes>
Otherwise it didnt work.
The program compiles now, but now your first example doesn't work. It returns a blank value instead of the int. It worked for Arduino however.
Also, I cant figure out where to put extern 'C' { without causing problems in my program. What exactly needs to be within the braces?
Thanks in advance for any help.
@jstamour802
I put the 'extern "C"' there only to tell you that this is C code and may need adaptations to run in wirish/C++, and obviously you have found out exactly that ;-)
So forget about the 'extern "C".
IIRC, the right way to include C headers stdlib.h, string.h or stdio.h in C++ is:
#include <cstdlib>
#include <cstring>
#include <cstdio>
I have actually tested both ways and they work fine here.
But I use the current launchpad GCC instead of the outdated codesourcery GCC...
thanks a ton for your help - I was able to get everything to work. This put me over a major hump in a project I am working on.
high five!
You must log in to post.