[plt-scheme] padding numbers

From: David Vanderson (david.vanderson at gmail.com)
Date: Wed Oct 15 20:33:04 EDT 2008

Hello,

The other day I wanted to convert an rgb triplet (say 0, 255, 0) into a 
hex representation padded with zeros (#00ff00).  I expected to be able 
to do something like (format "~02x~02x~02x" r g b), analogous to printf 
in C.

My attempts:
- srfi 48 format ~F only outputs in base 10, always pads with spaces
- srfi 54 cat automatically prepends "#o"
- srfi 13 string-pad:

#lang scheme
(require srfi/13)
(apply string-append (map (lambda (e)
                               (string-pad (format "~x" e) 2 #\0))
                           '(0 255 0)))

This was the best I could think of.  Is there a simpler way?

Thanks,
Dave


Posted on the users mailing list.