fun String.toByteArray( charset: Charset = Charsets.UTF_8 ): ByteArray
Encodes the contents of this string using the specified character set and returns the resulting byte array.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val charset = Charsets.UTF_8
val byteArray = "Hello".toByteArray(charset)
println(byteArray.contentToString()) // [72, 101, 108, 108, 111]
println(byteArray.toString(charset)) // Hello
//sampleEnd
}
© 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/to-byte-array.html