encodeToByteArray

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

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

Since Kotlin

1.4

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

expect fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray(source)

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

Since Kotlin

1.4

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.

Throws

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

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

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

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

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

Since Kotlin

1.4

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

actual fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray(source)

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

Since Kotlin

1.4

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.

Throws

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

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

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

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

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

Since Kotlin

1.4

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

actual fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray(source)

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

Since Kotlin

1.4

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.

Throws

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

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

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

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

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

Since Kotlin

1.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

actual fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray(source)

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

Since Kotlin

1.3

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.

Throws

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

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

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

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

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

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

actual fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray(source)

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

Since Kotlin

1.8

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.

Throws

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

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

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

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

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

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}

actual fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray(source)

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

Since Kotlin

1.8

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.

Throws

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

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

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val format = HexFormat { bytes { groupSeparator = " "; bytesPerGroup = 1 } }

// \u00a0 is a non-breaking space
val str = "Kòtlin\u00a02.1.255"

// The original string contains 14 characters, but some of them are represented with multiple UTF-8 code units
val byteArray = str.encodeToByteArray()
println(byteArray.toHexString(format)) // 4b c3 b2 74 6c 69 6e c2 a0 32 2e 31 2e 32 35 35

// Replacing all "wide" characters with some ASCII ones results in a byte sequence matching the length of the string
val byteArrayWithAsciiCharacters = str.replace("\u00a0", " ").replace("ò", "o").encodeToByteArray()
println(byteArrayWithAsciiCharacters.toHexString(format)) // 4b 6f 74 6c 69 6e 20 32 2e 31 2e 32 35 35

val byteArrayWithVersion = str.encodeToByteArray(startIndex = 7)
println(byteArrayWithVersion.toHexString(format)) // 32 2e 31 2e 32 35 35

val byteArrayWithoutTheVersion = str.encodeToByteArray(startIndex = 0, endIndex = 6)
println(byteArrayWithoutTheVersion.toHexString(format)) // 4b c3 b2 74 6c 69 6e 
   //sampleEnd
}