- How to Convert to and from Base64 in Kotlin
- Base64
- Types
- Default
- Functions
- decode
- decodeIntoByteArray
- Introduction
- What is Base64 Encoding?
- How to use Base64 Encoding in Kotlin
- Encoding a String
- Encoding a File
- Decoding a String
- Decoding a File
- Conclusion
- Saved searches
- Use saved searches to filter your results more quickly
- License
- saschpe/Kase64
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
How to Convert to and from Base64 in Kotlin
Base64 is a binary-to-text encoding scheme that is commonly used to represent arbitrary binary data in a text format. This is useful when you need to transfer binary data, such as images or other files, over a network or other communication channel that is designed to handle only text data.
In Kotlin, you can easily convert data to and from Base64 using the Base64 class from the standard library. This class provides static methods for encoding and decoding data in Base64 format.
Here’s an example of how to encode a string in Base64 format:
1import java.util.Base642 3val input = "Hello, world!"4val encoded = Base64.getEncoder().encodeToString(input.toByteArray())5println(encoded) // Output: "SGVsbG8sIHdvcmxkIQ=="
The getEncoder() method returns a Base64.Encoder object that you can use to encode data. The encodeToString() method takes the input data (in this case, a ByteArray containing the bytes of the input string) and returns the Base64-encoded version of the data as a String.
To decode a Base64-encoded string, you can use the getDecoder() method to get a Base64.Decoder object, and then call the decode() method on that object:
1val decoded = Base64.getDecoder().decode(encoded)2println(String(decoded)) // Output: "Hello, world!"
The decode() method takes a String containing the Base64-encoded data and returns a ByteArray containing the decoded data. In this example, we convert the ByteArray to a String using the String class’s constructor, which takes a ByteArray as its argument.
In addition to encoding and decoding strings, the Base64 class also provides methods for encoding and decoding other types of data, such as ByteArrays, InputStreams, and OutputStreams. You can find more information about these methods in the Android documentation.
In summary, the Base64 class in Kotlin makes it easy to convert data to and from Base64 format. This can be useful when you need to transfer binary data over a network or other communication channel that is designed to handle only text data.
Base64
This class is not supposed to be instantiated or inherited. However, predefined instances of this class are available for use. The companion object Base64.Default is the default instance of Base64. There are also Base64.UrlSafe and Base64.Mime instances.
Types
Default
The «base64» encoding specified by RFC 4648 section 4 , Base 64 Encoding.
Functions
decode
Decodes symbols from the specified source array or its subrange. Returns a ByteArray containing the resulting bytes.
Decodes symbols from the specified source char sequence or its substring. Returns a ByteArray containing the resulting bytes.
fun decode (
source : CharSequence ,
startIndex : Int = 0 ,
endIndex : Int = source.length
) : ByteArray
decodeIntoByteArray
Decodes symbols from the specified source array or its subrange and writes resulting bytes into the destination array. Returns the number of bytes written.
fun decodeIntoByteArray (
source : ByteArray ,
destination : ByteArray ,
destinationOffset : Int = 0 ,
startIndex : Int = 0 ,
endIndex : Int = source.size
) : Int
Decodes symbols from the specified source char sequence or its substring and writes resulting bytes into the destination array. Returns the number of bytes written.
fun decodeIntoByteArray (
source : CharSequence ,
destination : ByteArray ,
destinationOffset : Int = 0 ,
startIndex : Int = 0 ,
endIndex : Int = source.length
) : Int
Introduction
Base64 Encoding is a method of encoding binary data into a text format. It is commonly used to encode data that needs to be transmitted over the internet or stored in a database. Base64 Encoding is often used to encode binary files such as images or videos into a text format that can be easily transmitted over the internet. In this article, we will discuss how to use Base64 Encoding in Kotlin.
What is Base64 Encoding?
Base64 Encoding is a form of encoding binary data into a text format. It is a standard way of encoding binary data into a text format so that it can be transmitted over the internet. It is commonly used to encode images, videos, and other binary files into a text format.
How to use Base64 Encoding in Kotlin
In Kotlin, we can use the Base64 class to encode and decode data. We can use the encodeToString and decode methods to encode and decode the data in Base64 format.
Encoding a String
We can use the encodeToString method to encode a String into Base64 format. The following example shows how to encode a String into Base64 format in Kotlin.
val encodedString = Base64.encodeToString("Hello World".toByteArray(), Base64.DEFAULT)
The encodeToString method takes two parameters. The first parameter is the byte array that needs to be encoded. The second parameter is the encoding type. We can use the Base64.DEFAULT constant to specify the default encoding type.
Encoding a File
We can use the encode method to encode a file into Base64 format. The following example shows how to encode a file into Base64 format in Kotlin.
val file = File("sample.txt") val encodedFile = Base64.encode(file.readBytes(), Base64.DEFAULT)
The encode method takes two parameters. The first parameter is the byte array that needs to be encoded. The second parameter is the encoding type. We can use the Base64.DEFAULT constant to specify the default encoding type.
Decoding a String
We can use the decode method to decode a String from Base64 format. The following example shows how to decode a String from Base64 format in Kotlin.
val decodedString = Base64.decode(encodedString, Base64.DEFAULT)
The decode method takes two parameters. The first parameter is the encoded String. The second parameter is the encoding type. We can use the Base64.DEFAULT constant to specify the default encoding type.
Decoding a File
We can use the decode method to decode a file from Base64 format. The following example shows how to decode a file from Base64 format in Kotlin.
val decodedFile = Base64.decode(encodedFile, Base64.DEFAULT) val restoredFile = File("restored.txt") restoredFile.writeBytes(decodedFile)
The decode method takes two parameters. The first parameter is the encoded file. The second parameter is the encoding type. We can use the Base64.DEFAULT constant to specify the default encoding type.
Conclusion
In this article, we discussed how to use Base64 Encoding in Kotlin. We discussed how to encode and decode Strings and files in Base64 format. We used the Base64 class to encode and decode the data. We used the encodeToString and decode methods to encode and decode the data respectively.
Copyright © 2022 helpful.codes
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Base64 encoder/decoder for Kotlin/Multiplatform. Supports Android, iOS, JavaScript and plain JVM environments.
License
saschpe/Kase64
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Base64 encoder/decoder for Kotlin/Multiplatform. Supports Android, iOS, Linux, JavaScript, Windows, watchOS, tvOS and plain JVM environments.
Supported encodings
Artifacts are published to Maven Central:
repositories < mavenCentral() >dependencies < implementation("de.peilicke.sascha:kase64:1.0.6") >
Standard Base64 decoding and encoding:
import saschpe.kase64.* val helloWorld = "SGVsbG8sIHdvcmxkIQ==".base64Decoded // "Hello, world!" println("Hello, world!".base64Encoded) // Prints "SGVsbG8sIHdvcmxkIQ= auto">URL-safe Base64 decoding and encoding:import saschpe.kase64.* val helloWorld = "SGVsbG8sIHdvcmxkIQ".base64UrlDecoded // "Hello, world!" println("Hello, world!".base64UrlEncoded) // Prints "SGVsbG8sIHdvcmxkIQ"Copyright 2022 Sascha Peilicke Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
About
Base64 encoder/decoder for Kotlin/Multiplatform. Supports Android, iOS, JavaScript and plain JVM environments.