Below is a list of libraries that support Base64 encoding and decoding in Kotlin along with sample code for each library.
1. Java Standard Library (java.util.Base64)
The Java Standard Library provides the java.util.Base64
class for Base64 encoding and decoding, which can be used in Kotlin.
import java.util.Base64
fun main() {
val originalString = "test input"
// Encode
val encodedString = Base64.getEncoder().encodeToString(originalString.toByteArray())
println("Encoded: $encodedString")
// Decode
val decodedBytes = Base64.getDecoder().decode(encodedString)
val decodedString = String(decodedBytes)
println("Decoded: $decodedString")
}
2. Apache Commons Codec
The Apache Commons Codec library provides utilities for Base64 encoding and decoding, which can be used in Kotlin.
import org.apache.commons.codec.binary.Base64
fun main() {
val originalString = "test input"
// Encode
val encodedString = Base64.encodeBase64String(originalString.toByteArray())
println("Encoded: $encodedString")
// Decode
val decodedBytes = Base64.decodeBase64(encodedString)
val decodedString = String(decodedBytes)
println("Decoded: $decodedString")
}
Add the dependency to build.gradle
(Gradle):
dependencies {
implementation 'commons-codec:commons-codec:1.15'
}
3. Bouncy Castle
Bouncy Castle is a powerful encryption library that supports Base64 encoding and decoding, which can be used in Kotlin.
import org.bouncycastle.util.encoders.Base64
fun main() {
val originalString = "test input"
// Encode
val encodedString = String(Base64.encode(originalString.toByteArray()))
println("Encoded: $encodedString")
// Decode
val decodedBytes = Base64.decode(encodedString.toByteArray())
val decodedString = String(decodedBytes)
println("Decoded: $decodedString")
}
Add the dependency to build.gradle
(Gradle):
dependencies {
implementation 'org.bouncycastle:bcprov-jdk15on:1.70'
}
4. Kotlinx Serialization
Kotlinx Serialization library also supports Base64 encoding and decoding.
import kotlinx.serialization.encodeToString
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToByteArray
import kotlinx.serialization.decodeFromByteArray
@Serializable
data class Data(val content: String)
fun main() {
val originalData = Data("test input")
// Encode
val encodedString = Base64.getEncoder().encodeToString(Json.encodeToByteArray(originalData))
println("Encoded: $encodedString")
// Decode
val decodedBytes = Base64.getDecoder().decode(encodedString)
val decodedData = Json.decodeFromByteArray(decodedBytes)
println("Decoded: ${decodedData.content}")
}
Add the dependency to build.gradle
(Gradle):
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3"
}
5. kotlin-stdlib
The Kotlin Standard Library itself provides basic support for Base64 encoding and decoding using extension functions.
import java.util.Base64
fun ByteArray.toBase64(): String = Base64.getEncoder().encodeToString(this)
fun String.fromBase64(): ByteArray = Base64.getDecoder().decode(this)
fun main() {
val originalString = "test input"
// Encode
val encodedString = originalString.toByteArray().toBase64()
println("Encoded: $encodedString")
// Decode
val decodedString = String(encodedString.fromBase64())
println("Decoded: $decodedString")
}
Welcome to our Kotlin Base64 Encode tool, the ultimate solution to Kotlin Base64 Encode quickly and easily. Whether you need to convert binary data for safe transmission or storage, our tool provides a simple and efficient way to decode your data from Base64 format.
Why Use Base64 Encoding?
Base64 encoding is a method of converting binary data into a text format using 64 characters. This encoding scheme is especially useful when you need to transfer or store data over media designed to handle text, such as email or JSON files. Base64 ensures your data remains intact and unmodified during transport.
Key Features of Our Base64 Decoding Tool
Simple and Fast Decoding
Our tool makes it incredibly easy to Kotlin Base64 Encode. Simply enter your data, click the "Decode" button, and get your Base64 decoded result instantly. No complex configurations or technical knowledge required.
Secure and Private
We prioritize your privacy and data security. All communications with our servers are encrypted using SSL connections. We do not store or inspect the contents of your decoded data, ensuring complete confidentiality.
Free to Use
Our Base64 decoding tool is completely free. You don’t need to download any software or register for an account. Access our online tool anytime, anywhere, without any cost.
Advanced Options
Character Set Selection
While Base64 encoding does not inherently include character set information, our tool allows you to specify the character set used during encoding. Commonly, UTF-8 is used, but other options are available. If unsure, try the auto-detect option for the best results.
Live Decoding Mode
Enable live mode to see your data decoded in real-time using your browser’s built-in JavaScript functions. This feature supports the UTF-8 character set and does not send data to our servers, offering enhanced privacy.
How to Kotlin Base64 Encode
Follow these simple steps to Kotlin Base64 Encode:
- Enter your data:Paste your text or binary data into the input field.
- Click "Decode":Hit the "Decode" button to convert your data from Base64 format.
- Copy the result:Your decoded data will appear instantly. Copy it for use in your applications.
Example of Base64 Decoding
Here’s a quick example. Decoding the Base64 string "SGVsbG8sIFdvcmxkIQ==" yields:
Hello, World!
In this example, the Base64 encoded string "SGVsbG8sIFdvcmxkIQ==" is decoded to "Hello, World!", providing a simple and efficient way to convert Base64 data back to its original text format.