W3cubDocs

/Kotlin

encodeToByteArray

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
fun String.encodeToByteArray(): ByteArray

Encodes this string to an array of bytes in UTF-8 encoding.

Any malformed char sequence is replaced by the replacement byte sequence.

Platform and version requirements: JVM (1.4), JS (1.4)
fun String.encodeToByteArray(
    startIndex: Int = 0, 
    endIndex: Int = this.length, 
    throwOnInvalidSequence: Boolean = false
): ByteArray
Platform and version requirements: Native (1.3)
fun String.encodeToByteArray(
    startIndex: Int, 
    endIndex: Int, 
    throwOnInvalidSequence: Boolean
): ByteArray

Encodes this string or its substring to an array of bytes in UTF-8 encoding.

Parameters

startIndex - the beginning (inclusive) of the substring to encode, 0 by default.

endIndex - the end (exclusive) of the substring to encode, length of this string by default.

throwOnInvalidSequence - specifies whether to throw an exception on malformed char sequence or replace.

Exceptions

IndexOutOfBoundsException - if startIndex is less than zero or endIndex is greater than the length of this string.

IllegalArgumentException - if startIndex is greater than endIndex.

CharacterCodingException - if this string contains malformed char sequence and throwOnInvalidSequence is true.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/encode-to-byte-array.html