In Java, you can convert a string into an array of its constituent bytes by calling myString.getBytes().
What's the equivalent of this in Kotlin?
To convert a string to byte array in Kotlin, use String. toByteArray() method. String. toByteArray() method returns a Byte Array created using the characters of the calling string.
The method getBytes() encodes a String into a byte array using the platform's default charset if no argument is passed. We can pass a specific Charset to be used in the encoding process, either as a String object or a String object.
To create a Byte Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.
Use the extension String.toByteArray() - which means you will write myString.toByteArray(). Note it defaults to UTF-8 encoding, but you can override this by providing an additional argument.
For Kotlin use String.toBytesArray(), it will be same as String.getBytes() in Java.
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