Base64 encoding and decoding in Kotlin
This post will discuss Base64 encoding and decoding in Kotlin.
1. Using Base64 class
The idea is to use the encodeToString() function from Base64.Encoder for encoding the specified byte array into the base64 encoded string. To decode the base64 encoded string back to the byte array, use the decode() function from the Base64.Decoder class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Base64 fun main() { val string = "Some text" // encode a string using Base64 encoder val encoder: Base64.Encoder = Base64.getEncoder() val encoded: String = encoder.encodeToString(string.toByteArray()) println("Encoded Data: $encoded") // decode the encoded data val decoder: Base64.Decoder = Base64.getDecoder() val decoded = String(decoder.decode(encoded)) println("Decoded Data: $decoded") } |
Output:
Encoded Data: U29tZSB0ZXh0
Decoded Data: Some text
You can’t use a basic encoding to encode an URL. You should use the “URL and Filename safe” type Base64 encoding and decoding scheme. The following code example demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Base64 fun main() { val url = "https://techiedelight.com/" val encoded = Base64.getUrlEncoder().encodeToString(url.toByteArray()) println("Encoded URL: $encoded") val decoded = String(Base64.getUrlDecoder().decode(encoded)) println("Decoded URL: $decoded") } |
Output:
Encoded URL: aHR0cHM6Ly93d3cudGVjaGllZGVsaWdodC5jb20v
Decoded URL: https://techiedelight.com/
To encode a large block of text using the MIME encoder, use getMimeEncoder() and getMimeDecoder() functions.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.* fun main() { val buffer = StringBuilder() for (i in 0..4) { buffer.append(UUID.randomUUID().toString()) } val string = buffer.toString() val mimeEncoded = Base64.getMimeEncoder().encodeToString(string.toByteArray()) val mimeDecoded = String(Base64.getMimeDecoder().decode(mimeEncoded)) println(mimeDecoded) } |
Output (may vary):
e2317f97-47d0-4799-b383-2a89070d5c1ef57d1659-3153-47d6-88a1-be3d5cca8cf526afb83c-c0db-4ef6-bd41-edc7ca4a691db744a484-f6fd-4f80-a7e4-4d8d3d7d3153309b3b8c-d4b3-4027-821b-85450c735616
2. Using DatatypeConverter class
The DatatypeConverter class provides printBase64Binary() and parseBase64Binary() functions, which can be used to convert an array of bytes into a string and vice-versa.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import javax.xml.bind.DatatypeConverter fun main() { val string = "Some text" // encode a string using `Base64` encoder val encoded = DatatypeConverter.printBase64Binary(string.toByteArray()) println("Encoded String: $encoded") // decode the encoded data val decoded = String(DatatypeConverter.parseBase64Binary(encoded)) println("Decoded String: $decoded") } |
Output:
Encoded String: U29tZSB0ZXh0
Decoded String: Some text
That’s all about base64 encoding and decoding in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)