I am trying to convert String
to ByteArray
in Kotlin.
In Java we can convert using getBytes()
method.
To create a Byte Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.
bytearray() Syntaxbytearray() method returns a bytearray object (i.e. array of bytes) which is mutable (can be modified) sequence of integers in the range 0 <= x < 256 . If you want the immutable version, use the bytes() method.
You can use this too - JSON string:
val payLoad = "{\"Hello \":\"Paris\",\"countryCode\":\"FR\"}"
val byteArray = payLoad.encodeToByteArray()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With