-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
38 lines (32 loc) · 805 Bytes
/
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
package main
import (
"fmt"
"math/big"
"os"
"strconv"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Please input a decimal or hexadecimal number.")
os.Exit(1)
}
input := os.Args[1]
// If the number starts with 0x, we assume it's hexadecimal
if len(input) > 2 && input[0:2] == "0x" {
result := new(big.Int)
_, success := result.SetString(input[2:], 16)
if !success {
fmt.Println("Failed to parse the hexadecimal number")
os.Exit(1)
}
fmt.Printf("Hexadecimal %v is equal to decimal %v\n", input, result)
} else {
// Otherwise, we assume it's decimal
result, err := strconv.Atoi(input)
if err != nil {
fmt.Printf("Failed to parse the decimal number: %v\n", err)
os.Exit(1)
}
fmt.Printf("Decimal %v is equal to hexadecimal 0x%x\n", input, result)
}
}