Skip to content

Instantly share code, notes, and snippets.

@mustafaturan
Created June 25, 2018 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafaturan/87b22d2408d95a079610d1eac4edd0ce to your computer and use it in GitHub Desktop.
Save mustafaturan/87b22d2408d95a079610d1eac4edd0ce to your computer and use it in GitHub Desktop.
Base62 in Elixir
defmodule Base62 do
@moduledoc """
Base62 encoder
Copied from: https://github.com/otobus/event_bus/commit/956ffd93d854fb3a721aa7763c3da509cffedd41#diff-31f9094877d525a1b8387d4135042006R1
"""
@mapping '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
@doc """
Converts given integer to base62
"""
@spec encode(integer()) :: String.t()
def encode(num) when num < 62 do
<< Enum.at(@mapping, num) >>
end
def encode(num) do
encode(div(num, 62)) <> encode(rem(num, 62))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment