-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.go
executable file
·82 lines (72 loc) · 1.49 KB
/
bytes.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
78
79
80
81
82
package utils
import (
"encoding/hex"
"math/big"
)
// use to look up number of 1 bit in 4 bits
var halfByteLookup = [16]int{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}
// FromHex returns the bytes represented by the hexadecimal string s.
// s may be prefixed with "0x".
func FromHex(s string) []byte {
if len(s) > 1 {
if s[0:2] == "0x" || s[0:2] == "0X" {
s = s[2:]
}
}
if len(s)%2 == 1 {
s = "0" + s
}
return Hex2Bytes(s)
}
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str)
return h
}
// counts number of one bits in a byte
func countOneBitsInByte(by byte) int {
return halfByteLookup[by&0x0F] + halfByteLookup[by>>4]
}
// CountOneBits counts the number of 1 bit in byte array
func CountOneBits(arr []byte) int64 {
if arr == nil {
return 0
}
if len(arr) == 0 {
return 0
}
count := 0
for i := range arr {
count += countOneBitsInByte(arr[i])
}
return int64(count)
}
func BytesMiddle(a, b []byte) []byte {
if a == nil && b == nil {
return []byte{128}
}
if len(a) > len(b) {
tmp := make([]byte, len(a))
if b == nil {
for i, _ := range tmp {
tmp[i] = 255
}
}
copy(tmp, b)
b = tmp
} else if len(a) < len(b) {
tmp := make([]byte, len(b))
if a == nil {
for i, _ := range tmp {
tmp[i] = 0
}
}
copy(tmp, a)
a = tmp
}
aI := big.NewInt(0).SetBytes(a)
bI := big.NewInt(0).SetBytes(b)
aI.Add(aI, bI)
aI.Div(aI, big.NewInt(2))
return aI.Bytes()
}