Created
March 12, 2013 05:18
-
-
Save dc0d/5140546 to your computer and use it in GitHub Desktop.
encode and decode from different number base; for example 9876543210 in base 36 would be 4jc8lii. I've learnt this from python code at http://stackoverflow.com/questions/1119722/base-62-conversion-in-python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sample_usage() { | |
digits := "0123456789abcdefghijklmnopqrstuvwxyz" // base 36 | |
var num int64 = 9876543210 | |
fmt.Printf("input number is %d\n", num) | |
e := encode(num, digits) | |
fmt.Printf("encoded as %v\n", e) | |
d := decode(e, digits) | |
fmt.Printf("decoded as %v\n", d) | |
} | |
func decode(enced string, digits string) (ed int64) { | |
en := []rune(enced) | |
alf := []rune(digits) | |
base := len(alf) | |
lenen := len(en) | |
var num int64 | |
var ilf map[rune]int64 = make(map[rune]int64) | |
for k, v := range alf { | |
ilf[v] = int64(k) | |
} | |
for i, vn := range en { | |
power := lenen - (i + 1) | |
num += ilf[vn] * int64(math.Pow(float64(base), float64(power))) | |
} | |
ed = num | |
return | |
} | |
func encode(num int64, digits string) (ed string) { | |
ab := []rune(digits) | |
if num == 0 { | |
ed = string(ab[0]) | |
return | |
} | |
var res []rune | |
base := int64(len(ab)) | |
for num > 0 { | |
rem := num % base | |
num = num / base | |
res = append(res, ab[rem]) | |
} | |
res = reverseRuneSlice(res) | |
ed = string(res) | |
return | |
} | |
func reverseRuneSlice(slc []rune) (ed []rune) { | |
l := len(slc) | |
ed = make([]rune, l) | |
copy(ed, slc) | |
for i := 0; i < l/2; i++ { | |
ed[i], ed[l-i-1] = ed[l-i-1], ed[i] | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another usage I've encountered recently is generating Natural Area Code
which excludes vowels and avoids potential confusion between "0" (zero) and "O" (capital "o"), and "1" (one) and "I" (capital "i")
. (http://en.wikipedia.org/wiki/Natural_Area_Code)