Skip to content

Instantly share code, notes, and snippets.

@clsr
Created November 25, 2014 10:35
Show Gist options
  • Save clsr/ca15625de3dcfd4d4316 to your computer and use it in GitHub Desktop.
Save clsr/ca15625de3dcfd4d4316 to your computer and use it in GitHub Desktop.
Convert integers between different bases
package main
import (
"flag"
"fmt"
"github.com/clsr/intcodec"
"os"
"strings"
)
var (
inBase = flag.Int("i", 10, "the base of the input numbers")
outBase = flag.Int("o", 10, "the base of the output numbers")
upper = flag.Bool("u", false, "use uppercase letters in the output")
)
func main() {
flag.Parse()
in := intcodec.LowerBaseN(*inBase)
var out *intcodec.Codec
if *upper {
out = intcodec.UpperBaseN(*outBase)
} else {
out = intcodec.LowerBaseN(*outBase)
}
for _, arg := range flag.Args() {
n, err := in.DecodeInt(strings.ToLower(arg))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
fmt.Println(out.EncodeInt(n))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment