1. Overview

In this tutorial, we’ll discuss how to convert a String to a byte array in Kotlin. Additionally, we’ll see how to convert a substring to the byte array. Finally, we’ll describe the opposite conversion, from the byte array to the String object.

2. Convert String to byte Array

The conversion from a String to a byte array is an everyday use case in programming languages. The Kotlin language provides a straightforward solution for this case.

The String class provides a toByteArray() method. It converts the String object to a byte array object. Moreover, it converts the object using the UTF-8 Charset. It’s possible to set the charset by providing a parameter to the toByteArray() method.

Let’s see it with an example:

fun `should convert string to byte array`() {
    val string = "Hello world"
    val byteArray = string.toByteArray()
    assertThat(byteArray).isEqualTo(byteArrayOf(72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100))

As a result, every character has its representation in the byte array.

Similarly, we can convert the String with a defined charset:

val byteArray = string.toByteArray(Charsets.ISO_8859_1)

3. Convert Substring to byte Array

Now, let’s have a look at how to convert a part of the String to a byte array. Similarly, there’s a dedicated method in the String class. The class provides an encodeToByteArray() method. It converts a String to a byte array as well. Moreover, the encodeToByteArray() method internally calls the toByteArray() method with charset UTF-8 as a parameter.

Additionally, the String class exposes an overloaded encodeToByteArray() method with three parameters:

  • startIndex – the beginning position of a substring, included
  • endIndex – the last position of a substring, not included
  • throwOnInvalidSequence – specifies if we want it to throw an exception on a malformed char sequence

The startIndex parameter equals zero, by default. On the other hand, the endIndex parameter equals the length of the String.

Let’s look at an example:

fun `should convert substring to byte array`() {
    val string = "Hello world"
    val byteArray = string.encodeToByteArray(0, 5)
    assertThat(byteArray).isEqualTo(byteArrayOf(72, 101, 108, 108, 111))

Here, we converted only the “Hello” word to the byte array.

4. Convert byte Array to String

After that, let’s have a look at how to do a conversion from the byte array to the String object. Again, the String class has built-in functionality to handle it:

fun `should convert byte array to string`() {
    val byteArray = byteArrayOf(72, 101, 108, 108, 111)
    val string = String(byteArray)

Here, the constructor converts an array of bytes to characters and returns them combined as a String. The conversion uses the UTF-8 charset for decoding.

5. Conclusion

In this short article, we discussed the String conversion to a byte array. We saw how to convert the whole String as well as its substring. Finally, we learned how to convert a byte array to a String object.

As always, the source code of the examples is available over on GitHub.

Comments are closed on this article!