In this tutorial, you shall learn how to convert a given byte array to a string in Kotlin, using String() constructor, with examples.
Kotlin – Convert Byte Array to String
To convert a byte array to string in Kotlin, use String() constructor. String() constructor can take a Byte Array as argument and return a new string formed with the bytes in the given array.
Syntax
The syntax to call String() constructor with Byte Array bytes
passed as argument is
</>
Copy
String(bytes)
Examples
1. Create a string from given byte array
In the following example, we take an array of bytes, and convert this byte array to string using String().
Main.kt
</>
Copy
fun main() {
val bytes = byteArrayOf(97, 98, 99, 65, 66, 67)
val str = String(bytes)
println(str)
}
Output
abcABC
Conclusion
In this Kotlin Tutorial, we learned how to convert a Byte Array to a String using String() constructor, with examples.