encodeToByteArray

fun encodeToByteArray(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): ByteArray(source)

Encodes bytes from the specified source array or its subrange. Returns a ByteArray containing the resulting symbols.

Whether the encoding result is padded with '=' depends on the PaddingOption set for this Base64 instance.

Each resulting symbol occupies one byte in the returned byte array.

Use encode to get the output in string form.

Since Kotlin

1.8

Return

a ByteArray with the resulting symbols.

Parameters

source

the array to encode bytes from.

startIndex

the beginning (inclusive) of the subrange to encode, 0 by default.

endIndex

the end (exclusive) of the subrange to encode, size of the source array by default.

Throws

when startIndex or endIndex is out of range of source array indices.

when startIndex > endIndex.

Samples

import kotlin.io.encoding.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val data = byteArrayOf(-1, 0, -2, 0, -3)

val encoded = Base64.encodeToByteArray(data)
println("encoded.contentEquals(\"/wD+AP0=\".encodeToByteArray()) is ${encoded.contentEquals("/wD+AP0=".encodeToByteArray())}") // true

val encodedFromSubRange = Base64.encodeToByteArray(data, startIndex = 1, endIndex = 3)
println("encodedFromSubRange.contentEquals(\"AP4=\".encodeToByteArray()) is ${encodedFromSubRange.contentEquals("AP4=".encodeToByteArray())}") // true 
   //sampleEnd
}