Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created March 20, 2013 03:48
Show Gist options
  • Select an option

  • Save valvallow/5202162 to your computer and use it in GitHub Desktop.

Select an option

Save valvallow/5202162 to your computer and use it in GitHub Desktop.
number->string
#!/usr/local/bin/gosh
(use gauche.parseopt)
(define (usage cmd)
(print "usage: " cmd " [option ...] number ...")
(print " " cmd " -r [option ...] number [obase ...]")
(print " options:")
(print " r|rest-to-obase : rest args to obase")
(print " i|ibase : input base (default 10)")
(print " o|obase : output base (default 2)")
(exit))
(define (main args)
(let-args (cdr args)
((help "h|help" => (cut usage (car args)))
(ibase "i|ibase=i" 10)
(obase "o|obase=i" 2)
(rest2obase "r|rest-to-obase")
(else (opt . _)
(print "Unknown option : " opt)
(usage (car args)))
. rest)
(cond ((null? rest)(usage (car args)))
((null? (cdr rest))
(print (number->string (string->number (car rest) ibase) obase)))
(rest2obase
(for-each (^o (print (number->string (string->number (car rest) ibase) (x->integer o))))
(cdr rest)))
(else (for-each (^s (print (number->string (string->number s ibase) obase))) rest)))))
@valvallow
Copy link
Author

% ns 2
10

% ns 2 3 4
10
11
100

% ns -o 16 20 30 40
14
1e
28

% ns -r 30 2 3 4 5 6 7 8 9 10
11110
1010
132
110
50
42
36
33
30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment