t3x.org / sketchy / library / numtostr.html
SketchyLISP
Reference
  Copyright (C) 2007
Nils M Holm

number->string

Conformance: R5RS Scheme (Restrictions: Accepts only strings representing integers. )

Purpose: Convert a number to a string. The radix must be in the range 2..16. If no radix is given, 10 is assumed.

Arguments:
N - integer
RADIX - optional radix

Implementation:

(define (number->string n . radix)
  (letrec
    ((digits
       (list->vector (string->list "0123456789abcdef")))
     (conv
       (lambda (n rdx res)
         (cond ((zero? n) res)
           (else (conv (quotient n rdx) rdx
                   (cons (vector-ref digits (remainder n rdx))
                         res))))))
     (get-radix
       (lambda ()
         (cond ((null? radix) 10)
           ((< 1 (car radix) 17) (car radix))
           (else (bottom '(bad radix in number->string)))))))
    (let ((r (get-radix)))
      (cond ((= r 10) (write-to-string (+ n 0)))
        ((zero? n) "0")
        ((negative? n)
          (list->string
            (cons #\- (conv (abs n) r '()))))
        (else (list->string (conv n r '())))))))

Example:

(number->string -2748 16) 
=> "-abc"

See also:
string->number.