Base64 to byte array kotlin

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.

The symbols for decoding are not required to be padded. However, if there is a padding character present, the correct amount of padding character(s) must be present. The padding character ‘=’ is interpreted as the end of the encoded byte data. Subsequent symbols are prohibited.

Parameters

source — the array to decode symbols from.

destination — the array to write bytes into.

destinationOffset — the starting index in the destination array to write bytes to, 0 by default.

startIndex — the beginning (inclusive) of the subrange to decode, 0 by default.

endIndex — the end (exclusive) of the subrange to decode, size of the source array by default.

Exceptions

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

IllegalArgumentException — when startIndex > endIndex .

IndexOutOfBoundsException — when the resulting bytes don’t fit into the destination array starting at the specified destinationOffset, or when that index is out of the destination array indices range.

IllegalArgumentException — when the symbols for decoding are padded incorrectly or there are extra symbols after the padding.

Return the number of bytes written into destination array.

fun decodeIntoByteArray (
source : CharSequence ,
destination : ByteArray ,
destinationOffset : Int = 0 ,
startIndex : Int = 0 ,
endIndex : Int = source.length
) : Int
(source)

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.

The symbols for decoding are not required to be padded. However, if there is a padding character present, the correct amount of padding character(s) must be present. The padding character ‘=’ is interpreted as the end of the encoded byte data. Subsequent symbols are prohibited.

Parameters

source — the char sequence to decode symbols from.

destination — the array to write bytes into.

destinationOffset — the starting index in the destination array to write bytes to, 0 by default.

startIndex — the beginning (inclusive) of the substring to decode, 0 by default.

endIndex — the end (exclusive) of the substring to decode, length of the source by default.

Exceptions

IndexOutOfBoundsException — when startIndex or endIndex is out of range of source indices.

IllegalArgumentException — when startIndex > endIndex .

IndexOutOfBoundsException — when the resulting bytes don’t fit into the destination array starting at the specified destinationOffset, or when that index is out of the destination array indices range.

IllegalArgumentException — when the symbols for decoding are padded incorrectly or there are extra symbols after the padding.

Return the number of bytes written into destination array.

Источник

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

Источник

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.Base64
2
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.

Источник

hrules6872 / Base64.kt

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

fun String. encodeBase64ToString (): String = String ( this .toByteArray().encodeBase64())
fun String. encodeBase64ToByteArray (): ByteArray = this .toByteArray().encodeBase64()
fun ByteArray. encodeBase64ToString (): String = String ( this .encodeBase64())
fun String. decodeBase64 (): String = String ( this .toByteArray().decodeBase64())
fun String. decodeBase64ToByteArray (): ByteArray = this .toByteArray().decodeBase64()
fun ByteArray. decodeBase64ToString (): String = String ( this .decodeBase64())
fun ByteArray. encodeBase64 (): ByteArray
val table = ( CharRange ( ‘ A ‘ , ‘ Z ‘ ) + CharRange ( ‘ a ‘ , ‘ z ‘ ) + CharRange ( ‘ 0 ‘ , ‘ 9 ‘ ) + ‘ + ‘ + ‘ / ‘ ).toCharArray()
val output = ByteArrayOutputStream ()
var padding = 0
var position = 0
while (position < this .size)
var b = this [position].toInt() and 0xFF shl 16 and 0xFFFFFF
if (position + 1 < this .size) b = b or ( this [position + 1 ].toInt() and 0xFF shl 8 ) else padding ++
if (position + 2 < this .size) b = b or ( this [position + 2 ].toInt() and 0xFF ) else padding ++
for (i in 0 until 4 — padding)
val c = b and 0xFC0000 shr 18
output.write(table[c].toInt())
b = b shl 6
>
position + = 3
>
for (i in 0 until padding)
output.write( ‘ = ‘ .toInt())
>
return output.toByteArray()
>
fun ByteArray. decodeBase64 (): ByteArray
val table = intArrayOf( — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 ,
— 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , 62 , — 1 , — 1 , — 1 , 63 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , — 1 , — 1 , — 1 ,
— 1 , — 1 , — 1 , — 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , — 1 , — 1 , — 1 , — 1 , — 1 ,
— 1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 ,
— 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 ,
— 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 ,
— 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 ,
— 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 , — 1 )
val output = ByteArrayOutputStream ()
var position = 0
while (position < this .size)
var b : Int
if (table[ this [position].toInt()] != — 1 )
b = table[ this [position].toInt()] and 0xFF shl 18
> else
position ++
continue
>
var count = 0
if (position + 1 < this .size && table[ this [position + 1 ].toInt()] != - 1 )
b = b or (table[ this [position + 1 ].toInt()] and 0xFF shl 12 )
count ++
>
if (position + 2 < this .size && table[ this [position + 2 ].toInt()] != - 1 )
b = b or (table[ this [position + 2 ].toInt()] and 0xFF shl 6 )
count ++
>
if (position + 3 < this .size && table[ this [position + 3 ].toInt()] != - 1 )
b = b or (table[ this [position + 3 ].toInt()] and 0xFF )
count ++
>
while (count > 0 )
val c = b and 0xFF0000 shr 16
output.write(c.toChar().toInt())
b = b shl 8
count —
>
position + = 4
>
return output.toByteArray()
>

Источник

Читайте также:  What is sphinx in java
Оцените статью