Help on String.int2String

Posted by pikachu on
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/Help-on-String-int2String-tp4030582.html

Here is my current code for String.int2String:


        function String int2String(int n) {
                var int lastDigit, i;
                var String output, tmp;
                var int c;
                if (n < 0) {
                        let output = String.new(10);
                        let output = output.appendChar(45);
                        let tmp = String.int2String(-n);
                        let i = 0;
                        while (i < tmp.length()) {
                                let output = output.appendChar(tmp[i]);
                        }
                } else {
                        let lastDigit = n - (n / 10 * 10);
                        let c = n + 48;
                        if (n < 10) {
                                let output = String.new(10);
                        } else {
                                let output = String.int2String(n / 10);
                        }
                        let output = output.appendChar(c);
                }
                return output;
        }

I've tested this, and an input of 12345 gives a string like (ascii integers): 49|60|[other numbers over 1000], and I don't know why and fixing it proves difficult.