-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
75 lines (68 loc) · 1.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//go:generate usagen num
// num is a tool which displays the binary, octal, decimal and hexadecimal
// representation of numbers.
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func usage() {
const use = `
Usage: num BIN|OCT|DEC|HEX
Examples:
num 0b111101101
num 0o755
num 493
num 0x1ED
`
fmt.Fprint(os.Stderr, use[1:])
}
func main() {
// Parse command line arguments.
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
s := flag.Arg(0)
n, err := parseNum(s)
if err != nil {
log.Fatal(err)
}
const format = `
bin: 0b%b
oct: 0o%o
dec: %d
hex: 0x%X
`
fmt.Printf(format[1:], n, n, n, n)
}
// parseNum parses the given string as a binary, octal, decimal or hexadecimal
// number based on its prefix; with "0b", "0o", no prefix, and "0x"
// respectively.
func parseNum(s string) (n uint64, err error) {
var base int
switch {
case strings.HasPrefix(s, "0b"):
// Binary
base = 2
s = s[len("0b"):]
case strings.HasPrefix(s, "0o"):
// Octal
base = 8
s = s[len("0o"):]
case strings.HasPrefix(s, "0x"):
// Hexadecimal
base = 16
s = s[len("0x"):]
default:
// Decimal
base = 10
}
return strconv.ParseUint(s, base, 64)
}