This code has a bug somewhere: it does not correctly handle -32767 , but -7 is correct.
-----------------------------------------------------------------------------------------------------------------------------
method void int2String(int number) {
var int n, lastDigit, c ;
let n = number;
let lastDigit = n - (10 * Math.divide(n,10));
let c = 48 + lastDigit ;
if (n < 10) { do appendChar(c);
return ;}
else { do setInt(Math.divide(n,10));
do appendChar(c);return;}
}
/** Sets this String to hold a representation of the given number. */
method void setInt(int number) {
let length = 0;
if (number < 0) { do appendChar(45);
do int2String(-number);
return; }
else { do int2String(number);
return;}
}
-----------------------------------------------------------------------------------------------------------------------------
By calling int2String from within setInt the appended - prefix somehow gets lost.
Because the base case '-7' somehow works but negative integers > 10 not, maybe there is a problem with the recursion.
Also i am not sure what the return value of int2String should be : void or String ?
I studied all the Jack documentation over and over but i cannot find the material that would indicate where i have been mistaken.
I would appreciate some help on this.
--Chrit