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.

65 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package common
import (
"math"
"strconv"
)
//用于uuid加密
func StringToArray(input string) []int {
output := []int{}
for _, v := range input {
output = append(output, int(v))
}
for i, j := 0, len(output)-1; i < j; i, j = i+1, j-1 {
output[i], output[j] = output[j], output[i]
}
return output
}
func GetInput(input string) <-chan int {
out := make(chan int)
go func() {
for _, b := range StringToArray(input) {
out <- b
}
close(out)
}()
return out
}
func SQ(in <-chan int) <-chan int {
out := make(chan int)
var base, i float64 = 2, 0
go func() {
for n := range in {
out <- (n - 48) * int(math.Pow(base, i)) //48是“0”对应的ASCII码依次递增
i++
}
close(out)
}()
return out
}
// 转换成int
func ToInt(input string) int {
c := GetInput(input)
out := SQ(c)
sum := 0
for o := range out {
sum += o
}
return sum
}
// int转二进制
func ConverToBinary(n int) string {
res := ""
for ; n > 0; n /= 2 {
lsb := n % 2
res = strconv.Itoa(lsb) + res
}
return res
}