/me had some fun again...
http://code.google.com/p/alexanderbrevig/source/browse/trunk#trunk/Arduino/Utilities/StringToAnything#include <StringToAnything.h>
StringToAnything<float,8> readFloat;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available()){
int in = Serial.read();
if (in!=' '){
readFloat += in;
} else {
Serial.println( readFloat.convert() );
readFloat = ""; //reset
}
}
}
You can also do this:
#include <StringToAnything.h>
StringToAnything<float,8> converter;
void setup(){
Serial.begin(9600);
converter = "1000101";
Serial.print("1000101 is: ");
Serial.println(converter.convert(BIN));
}
void loop(){/*nothign to loop*/}
If you only want to use this as a converter on already compiled char arrays.
You can use
DEC BIN OCT and
HEX when converting. This is when you tell the StringToAnything object what kind of format the string is.
123 can be:
DEC = 123
BIN = 1 (binary mode rejects 23)
OCT = 83
HEX = 291
Additionally you can indicate negation by beginning the string with a '-' such as "-23.34" will evaluate to the negative float -23.34 is the StringToAnything is setup correctly for it, like this:
StringToAnything<float,5> sta = "-23.34";
float f = sta.convert();
The number five in the declaration is because you will need to allocate space for the '-' in addition to each number.
I might eliminate this need on request.
