不同的整数生成固定六位的字符串验证码
package main
import (
"container/list"
"fmt"
)
var baseStr string = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
var base []byte = []byte(baseStr)
func Base34(n uint64) []byte {
quotient := n
mod := uint64(0)
l := list.New()
for quotient != 0 {
mod = quotient % 34
quotient = quotient / 34
l.PushFront(base[int(mod)])
}
listLen := l.Len()
if listLen >= 6 {
res := make([]byte, 0, listLen)
for i := l.Front(); i != nil; i = i.Next() {
res = append(res, i.Value.(byte))
}
return res
} else {
res := make([]byte, 0, 6)
for i := 0; i < 6; i++ {
if i < 6-listLen {
res = append(res, base[0])
} else {
res = append(res, l.Front().Value.(byte))
l.Remove(l.Front())
}
}
return res
}
}
func main() {
res := Base34(25226)
fmt.Println("===============base:25226->", string(res))
res = Base34(4204052)
fmt.Println("===============base:4204052->", string(res))
res = Base34(1544513516)
fmt.Println("===============base:1544513516->", string(res))
}
有疑问加站长微信联系(非本文作者)