Skip to content

Instantly share code, notes, and snippets.

@shouth
Last active December 24, 2019 15:22
Show Gist options
  • Save shouth/6cebf2a53140637e56856df2806086d6 to your computer and use it in GitHub Desktop.
Save shouth/6cebf2a53140637e56856df2806086d6 to your computer and use it in GitHub Desktop.
object Base64 {
def main(args: Array[String]): Unit = {
val str = "input here"
val result = str.getBytes
.flatMap(b => (0 until 8).map(n => (b >> (7 - n)) & 1))
.grouped(6)
.map(b => b.indices.map(n => b(n) << (5 - n)).sum)
.map("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"(_))
.grouped(4)
.flatMap(_.padTo(4, '='))
.mkString
println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment