-
Notifications
You must be signed in to change notification settings - Fork 6
/
binary.go
77 lines (62 loc) · 1.22 KB
/
binary.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
76
77
package lib
import (
"strconv"
"strings"
)
//BinToOct
func BinToOct(b string) string {
return baseConvert(b, 2, 8)
}
//BinToDec
func BinToDec(b string) string {
return baseConvert(b, 2, 10)
}
//BinToHex
func BinToHex(b string) string {
return strings.ToUpper(baseConvert(b, 2, 16))
}
//DecToBin
func DecToBin(b string) string {
return baseConvert(b, 10, 2)
}
//DecToOct
func DecToOct(d string) string {
return baseConvert(d, 10, 8)
}
//DecToHex
func DecToHex(b string) string {
return strings.ToUpper(baseConvert(b, 10, 16))
}
//OctToBin
func OctToBin(b string) string {
return baseConvert(b, 8, 2)
}
//OctToDec
func OctToDec(b string) string {
return baseConvert(b, 8, 10)
}
//OctToHex
func OctToHex(b string) string {
return strings.ToUpper(baseConvert(b, 8, 16))
}
//HexToBin
func HexToBin(b string) string {
return baseConvert(b, 16, 2)
}
//HexToOct
func HexToOct(b string) string {
return baseConvert(b, 16, 8)
}
//HexToDec
func HexToDec(b string) string {
return baseConvert(b, 16, 10)
}
// baseConvert("12312", 10, 16)
// [2- 36]
func baseConvert(number string, fromBase, toBase int) string {
i, err := strconv.ParseInt(number, fromBase, 0)
if err != nil {
return ""
}
return strconv.FormatInt(i, toBase)
}