{"id":99,"date":"2007-09-23T14:09:14","date_gmt":"2007-09-23T19:09:14","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/code\/arduinowiring\/99"},"modified":"2008-01-15T21:29:39","modified_gmt":"2008-01-16T02:29:39","slug":"atoi-for-large-numbers","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/99\/","title":{"rendered":"atoi() for large numbers"},"content":{"rendered":"<p>Justin Downs wrote this routine to convert a large ASCII number to an array, as a workaround to the size limitation to atoi().<\/p>\n<p><!--more--><\/p>\n<pre>\/* code that gets serial into a array then parses ASCII array into\nsize restriction a long on arduino\njustin downs*\/\n\n\n\n\/\/for test\nint ledPin = 13;   \/\/ select the pin for the LED\nint val = 0;       \/\/ variable to store the data from the serial port\nint serbyte = 0;   \/\/ variable to store the VALID data from the port\n\nchar inString[100];\n\nvoid setup() {\n  Serial.begin(9600);        \/\/ connect to the serial port\n}\n\nvoid loop () {\n\n\nif (Serial.available()&gt;0){\n  Serial.println(SerialLook());\n }\n}\n\n \/\/end test stuff\n\n\/\/****************************\n\/\/ gets serial data into a array\nlong SerialLook() {\n  char ASCIIString [100]; \/\/ in string\n  char inByte='c'; \n  long number = 0; \/\/ return number\n  int stringPos = 0; \/\/ keeps track of places in number\n  if ('a' == Serial.read()){ \/\/ for id parsing of serial\n while(inByte !='b'){\n   inByte = Serial.read();\n\n   \/\/ save  ASCII numeric characters in string:\n   if ((inByte &gt;= '0') &#38;&#38; (inByte &lt;= '9')){\n    \/\/ Serial.println( inByte);\n     ASCIIString [stringPos] = inByte;\n     stringPos++;\n    \/\/ Serial.println( stringPos);\n     }\n    }\n   }\n   Serial.flush(); \/\/ flush junk\n   number = SerialToInt( ASCIIString,stringPos ); \/\/ convert captured string to an int\n     stringPos = 0; \/\/reset count num\n   return  number;\n}\n\n\/*****************\n the important parts of the function conversions\nare to have a inverting for loop go\nfrom last in array to first and call the plusBaseTen()function\neach time.\n*******************\/\n\n\/\/ hits up the number by base ten powered\n long timesBaseTen(int _move){ \/\/ the\n     long place = 1; \/\/base 10\n     int n = 0;\n     if( _move &lt; 1){ return 1;}\/\/ones place no change\n     else{\n         for( n= 0; n&lt;_move; n++){ place = place*10;}\n     }return place;\n } \n\n\n\n\/\/ converts a ten digit ASCII to int\nlong SerialToInt(char *_input, int _arrayLength){\n    long number=0;\n    long total=0;\n    int arrayLook=0;\n    int i = 0;\n    char s[]={\"h\"};    \/\/ work around\n    \/\/ call invert string for proper string power order\n    for(i =_arrayLength; i &gt;= 0;i--){\n     \/\/ picks out ASCII numbers for error check\n            if ((_input[i] &gt;= '0') &#38;&#38; (_input[i]&lt;= '9')){\n             s[0] = _input[i];\/\/ work around to get string arg\n             number = atoi(s);\/\/ ASCII to converts number\n             number =  (number * (timesBaseTen(arrayLook))); \/\/ puts number\nin its place\n             arrayLook++;\n             total = number+total;\n              }\n      }\n     return total; \n     }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Justin Downs wrote this routine to convert a large ASCII number to an array, as a workaround to the size limitation to atoi().<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}