Skip to content

Instantly share code, notes, and snippets.

@ldacosta
Last active January 3, 2016 21:39
Show Gist options
  • Save ldacosta/8522631 to your computer and use it in GitHub Desktop.
Save ldacosta/8522631 to your computer and use it in GitHub Desktop.
How to convert a String into Base36
def toBase36(str: String): Long = {
var id: Long = 0
for (c <- str) {
val d = if (c.isDigit) c.toInt - '0'.toInt
else c.toInt - 'A'.toInt + 10
id = (id * 36) + d
}
id
}
def fromBase36(idp: Long): String = {
var id = idp
if (id == 0) {
"0"
} else {
var str = ""
while (id > 0) {
val d = id % 36
val c = if (d >= 0 && d <= 9) {
(d + '0').toChar
} else {
(d - 10 + 'A').toChar
}
str = c + str
id /= 36
}
str
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment