Kotlin - How to convert String to ByteArray

You can use this too - JSON string:

val payLoad = "{\"Hello \":\"Paris\",\"countryCode\":\"FR\"}"
val byteArray = payLoad.encodeToByteArray()

Here is solution for this:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val checkString = "Good Morning"
        val byteString = checkString.toByteArray()
    }
}

You do it like this:

val testVal = "Good Morning"
val bytes = testVal.toByteArray()

Use String.toByteArray()

extension: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-byte-array.html