-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase62.go
59 lines (51 loc) · 1.17 KB
/
base62.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 base62
import (
"bytes"
"math"
)
// BASE62 should always be 0-9,a-z,A-Z
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const base = len(alphabet)
var balphabet = []byte(alphabet)
// Encode a positive integer to a Base62 token
func Encode(num int) []byte {
if num == 0 {
return []byte{alphabet[0]}
}
// result byte arr
var res []byte
// remainder of modulo
var rem int
// calculate base (should be 62)
// until num is == 0
for num != 0 {
// calculate remainder
rem = num % base
// calculate quotient
num = num / base
// push alphabet[rem] into res
res = append(res, alphabet[rem])
}
// reverse res
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
res[i], res[j] = res[j], res[i]
}
return res
}
// Decode a Base62 token to its original positive integer
func Decode(str []byte) int {
// calculate base (should be 62)
strlen := len(str)
num := 0
idx := 0
// until num is == 0
for _, c := range str {
// calculate remainder
power := (strlen - (idx + 1))
// calculate quotient
index := bytes.IndexByte(balphabet, c)
num += index * int(math.Pow(float64(base), float64(power)))
idx++
}
return num
}