Justin Downs wrote this routine to convert a large ASCII number to an array, as a workaround to the size limitation to atoi().
/* code that gets serial into a array then parses ASCII array into
size restriction a long on arduino
justin downs*/
//for test
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the data from the serial port
int serbyte = 0; // variable to store the VALID data from the port
char inString[100];
void setup() {
Serial.begin(9600); // connect to the serial port
}
void loop () {
if (Serial.available()>0){
Serial.println(SerialLook());
}
}
//end test stuff
//****************************
// gets serial data into a array
long SerialLook() {
char ASCIIString [100]; // in string
char inByte='c';
long number = 0; // return number
int stringPos = 0; // keeps track of places in number
if ('a' == Serial.read()){ // for id parsing of serial
while(inByte !='b'){
inByte = Serial.read();
// save ASCII numeric characters in string:
if ((inByte >= '0') && (inByte <= '9')){
// Serial.println( inByte);
ASCIIString [stringPos] = inByte;
stringPos++;
// Serial.println( stringPos);
}
}
}
Serial.flush(); // flush junk
number = SerialToInt( ASCIIString,stringPos ); // convert captured string to an int
stringPos = 0; //reset count num
return number;
}
/*****************
the important parts of the function conversions
are to have a inverting for loop go
from last in array to first and call the plusBaseTen()function
each time.
*******************/
// hits up the number by base ten powered
long timesBaseTen(int _move){ // the
long place = 1; //base 10
int n = 0;
if( _move < 1){ return 1;}//ones place no change
else{
for( n= 0; n<_move; n++){ place = place*10;}
}return place;
}
// converts a ten digit ASCII to int
long SerialToInt(char *_input, int _arrayLength){
long number=0;
long total=0;
int arrayLook=0;
int i = 0;
char s[]={"h"}; // work around
// call invert string for proper string power order
for(i =_arrayLength; i >= 0;i--){
// picks out ASCII numbers for error check
if ((_input[i] >= '0') && (_input[i]<= '9')){
s[0] = _input[i];// work around to get string arg
number = atoi(s);// ASCII to converts number
number = (number * (timesBaseTen(arrayLook))); // puts number
in its place
arrayLook++;
total = number+total;
}
}
return total;
}