Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - How to convert String to ByteArray

I am trying to convert String to ByteArray in Kotlin.

In Java we can convert using getBytes() method.

like image 700
Pradeep Bishnoi Avatar asked Nov 29 '17 06:11

Pradeep Bishnoi


People also ask

How do I make a byte array in Kotlin?

To create a Byte Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.

How is Bytearray defined?

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.


1 Answers

You can use this too - JSON string:

val payLoad = "{\"Hello \":\"Paris\",\"countryCode\":\"FR\"}"
val byteArray = payLoad.encodeToByteArray()
like image 144
smac2020 Avatar answered Oct 20 '22 22:10

smac2020