to Byte Array
inline fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray
Content copied to clipboard
Encodes the contents of this string using the specified character set and returns the resulting byte array.
Samples
import samples.*
import java.util.Locale
import kotlin.test.*
fun main() {
//sampleStart
val charset = Charsets.UTF_8
val byteArray = "Hello".toByteArray(charset)
assertPrints(byteArray.contentToString(), "[72, 101, 108, 108, 111]")
assertPrints(byteArray.toString(charset), "Hello")
//sampleEnd
}