-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfinity.go
59 lines (48 loc) · 1.41 KB
/
infinity.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
package infinity
import (
"fmt"
"log"
"math/big"
"os"
"strconv"
"strings"
)
// BytesToIntString interprets a slice of bytes as a single integer, and returns that integer as a decimal string.
// e.g. [65 66 67] => "4276803"
func BytesToIntString(b []byte) string {
i := &big.Int{}
i.SetBytes(b)
return i.String()
}
// IntStringToBytes takes a decimal integer in string format and returns the byte representation of the integer.
// e.g. "4276803" => [65 66 67]
func IntStringToBytes(s string) []byte {
i := &big.Int{}
_, ok := i.SetString(s, 10)
if !ok {
log.Fatalf("error creating big.Int from string: %s", s)
}
return i.Bytes()
}
// BinaryToDecimal takes a string of 0s and 1s and returns it as an integer.
func BinaryToDecimal(binary string) (int64, error) {
decimal, err := strconv.ParseInt(os.Args[1], 2, 63)
if err != nil {
return 0, fmt.Errorf("error parsing binary string %s: \n", err)
}
return decimal, nil
}
// DecimalToBinary takes a decimal string and returns it as a binary string of 0s and 1s.
func DecimalToBinary(decimal string) (string, error) {
d, err := strconv.ParseInt(decimal, 10, 64)
if err != nil {
return "", fmt.Errorf("error parsing decimal: %s\n", err)
}
return fmt.Sprintf("%0b", d), nil
}
// Removes indentation and newlines.
func RemoveWhitespace(s string) string {
result := strings.Replace(s, " ", "", -1)
result = strings.Replace(result, "\n", "", -1)
return result
}