[plt-scheme] formatting integer in string

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat Aug 17 05:01:42 EDT 2002

Chris Uzdavinis wrote:
>   For list-related administrative tasks:
>     http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>
> I'm unhappy with the solution I've found to a simple (in
> C at least) problem.
>
> I need write a 1-byte value (guaranteed range 0-256) into
> a 3-digit string, with leading zeros for padding if
> necessary.

This is a simple solution:

(define (byte->fixed-string b)
  (substring (number->string (+ b 1000)) 1 4))

(byte->fixed-string 0)
(byte->fixed-string 1)
(byte->fixed-string 10)
(byte->fixed-string 11)
(byte->fixed-string 100)
(byte->fixed-string 121)

gives

"000"
"001"
"010"
"011"
"100"
"121"

--
Jens Axel Søgaard





Posted on the users mailing list.