Skip to content

Instantly share code, notes, and snippets.

@ehmo
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save ehmo/ec4ab4b57578682a6ae8 to your computer and use it in GitHub Desktop.

Select an option

Save ehmo/ec4ab4b57578682a6ae8 to your computer and use it in GitHub Desktop.

Revisions

  1. Rastislav Turek revised this gist Jun 9, 2015. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions base62.go
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,6 @@ package main
    import (
    "fmt"
    "strings"
    "time"
    "math"
    )

    const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2. Rastislav Turek revised this gist Jun 9, 2015. 1 changed file with 1 addition and 18 deletions.
    19 changes: 1 addition & 18 deletions base62.go
    Original file line number Diff line number Diff line change
    @@ -23,11 +23,7 @@ func Pow(a, b int) int {
    return p
    }

    func Round(f float64) float64 {
    return math.Floor(f + .5)
    }

    func base62EncodeB(num int) []byte {
    func base62Encode(num int) []byte {

    code := make([]byte, 0, 128)

    @@ -45,19 +41,6 @@ func base62EncodeB(num int) []byte {
    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
  3. Rastislav Turek revised this gist Jun 9, 2015. 1 changed file with 28 additions and 15 deletions.
    43 changes: 28 additions & 15 deletions base62.go
    Original 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 base62Encode(num int) string {
    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]
    }

    alphabet := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    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 {

    alphabet := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    num := 0
    i := 0
    for _, char := range code {
    @@ -48,13 +70,4 @@ func base62Decode(code string) int {
    }

    return num
    }


    func main() {


    fmt.Println(base62Encode(1298478921395))
    fmt.Println(base62Decode("mRlze1B"))

    }
  4. Rastislav Turek created this gist Jun 9, 2015.
    60 changes: 60 additions & 0 deletions base62.go
    Original 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"))

    }