You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
878 B

package str
import (
"math"
"strings"
)
var Bit62Chars string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// 翻转[]byte
func ByteReverse(a []byte) {
for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 {
a[left], a[right] = a[right], a[left]
}
}
// int64 -> string
func (s *Str) Bit62Encode(num int64) string {
bytes := []byte{}
for num > 0 {
bytes = append(bytes, Bit62Chars[num%62])
num = num / 62
}
ByteReverse(bytes)
return string(bytes)
}
// string -> int64
func (s *Str) Bit62Decode(str string) int64 {
var num int64
n := len(str)
for i := 0; i < n; i++ {
pos := strings.IndexByte(Bit62Chars, str[i])
num += int64(math.Pow(62, float64(n-i-1)) * float64(pos))
}
return num
}
func (s *Str) Bit62Add(base, value string) string {
return s.Bit62Encode(s.Bit62Decode(base) + s.Bit62Decode(value))
}