-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhex.go
More file actions
38 lines (33 loc) · 764 Bytes
/
Copy pathhex.go
File metadata and controls
38 lines (33 loc) · 764 Bytes
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
package surl
import (
"math"
"strings"
)
// hexConvert
func hexConvert(num string, currentRadix, targetRadix int) (result string) {
ten := enten(num, currentRadix)
result = deten(ten, targetRadix)
return
}
// N to 10
func enten(num string, r int) (result int) {
for k, v := range strings.Split(reverse(num), "") {
result += strings.Index(chars, v) * int(math.Pow(float64(r), float64(k)))
}
return
}
// 10 to N
func deten(num int, r int) (result string) {
for i := num; i > 0; i /= r {
result = string(chars[i%r]) + result
}
return
}
// reverse
func reverse(s string) string {
runes := []rune(s)
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}