Created
November 25, 2014 10:35
-
-
Save clsr/ca15625de3dcfd4d4316 to your computer and use it in GitHub Desktop.
Convert integers between different bases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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