Last active
August 29, 2015 14:22
-
-
Save ehmo/ec4ab4b57578682a6ae8 to your computer and use it in GitHub Desktop.
Revisions
-
Rastislav Turek revised this gist
Jun 9, 2015 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,6 @@ package main import ( "fmt" "strings" ) const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" -
Rastislav Turek revised this gist
Jun 9, 2015 . 1 changed file with 1 addition and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,11 +23,7 @@ func Pow(a, b int) int { return p } func base62Encode(num int) []byte { code := make([]byte, 0, 128) @@ -45,19 +41,6 @@ func base62EncodeB(num int) []byte { return code } func base62Decode(code string) int { num := 0 -
Rastislav Turek revised this gist
Jun 9, 2015 . 1 changed file with 28 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,12 @@ package main import ( "fmt" "strings" "time" "math" ) const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // Integer power: compute a**b using binary powering algorithm // See Donald Knuth, The Art of Computer Programming, Volume 2, Section 4.6.3 func Pow(a, b int) int { @@ -19,25 +23,43 @@ func Pow(a, b int) int { return p } func Round(f float64) float64 { return math.Floor(f + .5) } func base62EncodeB(num int) []byte { code := make([]byte, 0, 128) for num > 0 { rem := num % len(alphabet) num = num / len(alphabet) code = append(code, alphabet[rem]) } for i, j := 0, len(code)-1; i < j; i, j = i+1, j-1 { code[i], code[j] = code[j], code[i] } return code } func base62Encode(num int) string { code := "" for num > 0 { rem := num % len(alphabet) num = num / len(alphabet) code = string(alphabet[rem]) + code } return code } func base62Decode(code string) int { num := 0 i := 0 for _, char := range code { @@ -48,13 +70,4 @@ func base62Decode(code string) int { } return num } -
Rastislav Turek created this gist
Jun 9, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ package main import ( "fmt" "strings" ) // Integer power: compute a**b using binary powering algorithm // See Donald Knuth, The Art of Computer Programming, Volume 2, Section 4.6.3 func Pow(a, b int) int { p := 1 for b > 0 { if b&1 != 0 { p *= a } b >>= 1 a *= a } return p } func base62Encode(num int) string { alphabet := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" code := "" for num > 0 { rem := num % len(alphabet) num = num / len(alphabet) code = string(alphabet[rem]) + code } return code } func base62Decode(code string) int { alphabet := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" num := 0 i := 0 for _, char := range code { power := (len(code) - (i + 1)) num = num + strings.Index(alphabet, string(char)) * Pow(len(alphabet), power) i++ } return num } func main() { fmt.Println(base62Encode(1298478921395)) fmt.Println(base62Decode("mRlze1B")) }