-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64_IntArray.go
53 lines (48 loc) · 1.11 KB
/
base64_IntArray.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
package nodeEncode
func IntArrayToBase64Str(intArray []int64) string {
if intArray == nil || len(intArray) == 0 {
return ""
} else {
return ToBase64(IntArrayToByteArray(intArray))
}
}
func IntArrayToByteArray(intArray []int64) []byte {
if intArray == nil || len(intArray) == 0 {
return []byte{}
} else {
byteArray := make([]byte, max(intArray)/8+1)
for _, v := range intArray {
quotient := v / 8
remainder := v % 8
byteArray[quotient] = byteArray[quotient] | (1 << remainder)
}
return byteArray
}
}
func Base64StrToIntArray(base64Str string) ([]int64, error) {
byteArray, err := ToByteArray(base64Str)
if err != nil {
return nil, err
}
return ByteArrayToIntArray(byteArray), nil
}
func ByteArrayToIntArray(byteArray []byte) []int64 {
intArray := make([]int64, 0)
for i := 0; i < len(byteArray); i++ {
for j := 0; j < 8; j++ {
if byteArray[i]&(1<<j) != 0 {
intArray = append(intArray, int64(j+8*i))
}
}
}
return intArray
}
func max(theArray []int64) (theMax int64) {
theMax = theArray[0]
for _, v := range theArray {
if v > theMax {
theMax = v
}
}
return theMax
}