baboomerang/Graphing-Calculator-Project

Conversion of Input to Bignumber

baboomerang opened this issue · 1 comments

The hardest thing to do without recursive addition to get the number.

Tokenizing part of a char array and initializing it as a big number does not work under any easy or simple conventional method in C.
char buf[50]; //holds serial input of chars

We cannot do BigNumber i = buf; despite the initializer syntax in the library supporting that.
It would only work if it was BigNumber i = ("<string-constructor>");

Using toINT() truncates the input prior to conversion.
char array to long by strtol() then long to big number seems possible, but convoluted and still truncates higher-than-long inputs.

The solution that I have used before was a recursive input stacked addition:
psuedocode
for(loop till delimiter(end of string)) {
get the char from the string
convert to int then - 48 (ASCII to decimal conversion)
multiply by that number by 10
then add the next calculated integer
keep doing that till you get your integer part of the number.

625868235 would be processed as
'6' first char then convert to int which is 54
54 - 48 to get int (6)
add 6 to the collector and multiply by 10

the collector is now 60
'2' second char then convert to int which is 50
50 - 48 to get int (2)
add 2 to the collector ( 60 ) to get 62
multiply by 10 again to get 620
...
...
..
.
do this till the last int but don't multiply by 10 to get your big number
inefficient I guess, and quite problematic, but is a necessary evil.

60 + 2 620 + 5 6520 + 8 65280 +6 652860 + 8 6528680 +2 65286820 + 3 ---> 652868235 entirely as a bignumber with no conversion problems.

SOLVED

fixed with

char buff [500];
int len = Zshort.length());
Zshort.toCharArray(buff, len);
BigNumber i = BigNumber(buff);