Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we’ll discuss how to create a byte array in Kotlin. We’ll see how the byte is represented in Kotlin. Additionally, we’ll show how to create the byte array using signed and unsigned bytes.

2. Bytes Representation in Kotlin

Firstly, let’s show how the byte is represented in Kotlin. Above all, the byte is represented by a Byte type. Additionally, the Byte type contains a signed value. It means that one bit is reserved for the information if a value is positive or negative. As a consequence, it can store a value between -128 and 127. Additionally, Kotlin supports an unsigned byte. The Ubyte type is provided as an experimental feature. It can store values between zero and 225.

3. Create a Byte Array Using Signed Byte

Let’s now have a look at how to create a byte array. Kotlin provides a built-in method byteArrayOf. Moreover, it takes as an argument multiple values of the Byte type.

Now, let’s show it in a simple example:

@Test
fun `create a byte array using signed byte`() {
    val byteArray = byteArrayOf(0x48, 101, 108, 108, 111)
    val string = String(byteArray)
    assertThat(string).isEqualTo("Hello")
}

An argument is a Byte object, which represents an 8-bit signed integer. We can use, i.e. hexadecimal values or integer values as arguments. Moreover, the values must be in the range of -128 and 127. Thanks to that, we can create an array of negative and positive values:

@Test
fun `create a byte array with negative values`() {
    val byteArray = byteArrayOf(Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE)
    assertThat(byteArray)
      .hasSize(5)
      .containsExactly(-128,-1, 0, 1, 127)
}

Consequently, when we provide a value outside of the range, it causes a compilation error: “The integer literal does not conform to the expected type Byte“.

4. Create a Byte Array Using Unsigned Byte

After that, let’s have a look at how to create a byte array using an unsigned byte. The Kotlin language provides an inline class UByteArray. The whole class is an experimental feature. It provides a method ubyteArrayOf, which creates a collection of Ubyte objects. Moreover, the method is an experimental feature as well. We should mark the usage with @OptIn(ExperimentalUnsignedTypes::class) or @ExperimentalUnsignedTypes to avoid compilation warnings

Let’s create an example:

@Test
@OptIn(ExperimentalUnsignedTypes::class)
fun `create a byte array using unsigned byte`(){
    val uByteArray = ubyteArrayOf(UByte.MIN_VALUE, 130U, 131u, UByte.MAX_VALUE)
    val intValues = uByteArray.map { it.toInt() }
    assertThat(intValues)
      .hasSize(4)
      .containsExactly(0, 130, 131, 255)
}

As shown above, we use U or u suffixes to declare unsigned values. Additionally, we can use only positive values from zero to 255 range. In case we would provide a value outside of the range, it causes a compilation error: “The integer literal does not conform to the expected type UByte“.

5. Conclusion

In this short article, we discussed byte array creation. We described byte representation in Kotlin. We saw how to create the byte array using signed bytes. Finally, we learned how to create the array using unsigned bytes.

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

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!