Skip to main content

Byte Structures

When we are working with data in form of bytes, we often need to interpret this data in a specific way. Two bytes can represent a short, four bytes can represent an integer, and so on.

Source Types

Primitives extends the functionality of the following types to allow for easy interpretation of data:

  • ByteArray
  • Array<Byte>
  • List<Byte> >> MutableList<Byte>

All operations are available for all three types.

Target Types

All data interpretations support the following target types:

Target TypeAliasSizeSample
Byte - 1
val bytes = byteArrayOf(0x01)
val byte = bytes.toByte() // >> 0x01

ShortBE

(short, big endian)

- 2
val bytes = byteArrayOf(0x01, 0x02)
val byte = bytes.toByte() // >> 0x0102

ShortLE

(short, little endian)

- 2
val bytes = byteArrayOf(0x01, 0x02)
val byte = bytes.toByte() // >> 0x0201

Short

(short, big endian)

- 2
val bytes = byteArrayOf(0x01, 0x02)
val short = bytes.toShort() // >> 0x0102

IntBE

(integer, big endian)

- 4
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
val short = bytes.toInt() // >> 0x01020304

IntLE

(integer, little endian)

- 4
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
val short = bytes.toInt() // >> 0x04030201

Int

(integer, big endian)

- 4
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
val short = bytes.toInt() // >> 0x01020304

LongBE

(long, big endian)

- 8
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val short = bytes.toLong() // >> 0x0102030405060708

LongLE

(long, little endian)

- 8
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val short = bytes.toLong() // >> 0x0807060504030201

Long

(long, big endian)

- 8
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val short = bytes.toLong() // >> 0x0102030405060708

FloatBE

(float, big endian)

- 4
val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00)
val short = bytes.toFloat() // >> 1.0

FloatLE

(float, little endian)

- 4
val bytes = byteArrayOf(0x00, 0x00, 0x80, 0x3F)
val short = bytes.toFloat() // >> 1.0

Float

(float, big endian)

- 4
val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00)
val short = bytes.toFloat() // >> 1.0

DoubleBE

(double, big endian)

- 8
val bytes = byteArrayOf(0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
val short = bytes.toDouble() // >> 1.0

DoubleLE

(double, little endian)

- 8
val bytes = byteArrayOf(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F)
val short = bytes.toDouble() // >> 1.0

Double

(double, big endian)

- 8
val bytes = byteArrayOf(0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
val short = bytes.toDouble() // >> 1.0

UByte

UnsignedByte 1
val bytes = byteArrayOf(0x01)
val byte = bytes.toUByte() // >> 0x01

UShortBE

(unsigned short, big endian)

UnsignedShortBE 2
val bytes = byteArrayOf(0x01, 0x02)
val byte = bytes.toUShort() // >> 0x0102

UShortLE

(unsigned short, little endian)

UnsignedShortLE 2
val bytes = byteArrayOf(0x01, 0x02)
val byte = bytes.toUShort() // >> 0x0201

UShort

(unsigned short, big endian)

UnsignedShort 2
val bytes = byteArrayOf(0x01, 0x02)
val short = bytes.toUShort() // >> 0x0102

UIntBE

(unsigned integer, big endian)

UnsignedIntBE 4
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
val short = bytes.toUInt() // >> 0x01020304

UIntLE

(unsigned integer, little endian)

UnsignedIntLE 4
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
val short = bytes.toUInt() // >> 0x04030201

UInt

(unsigned integer, big endian)

UnsignedInt 4
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
val short = bytes.toUInt() // >> 0x01020304

ULongBE

(unsigned long, big endian)

UnsignedLongBE 8
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val short = bytes.toULong() // >> 0x0102030405060708

ULongLE

(unsigned long, little endian)

UnsignedLongLE 8
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val short = bytes.toULong() // >> 0x0807060504030201

ULong

(unsigned long, big endian)

UnsignedLong 8
val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val short = bytes.toULong() // >> 0x0102030405060708

Operations

to

The to function converts the data to a type. This function needs the Array/ByteArray/List to be of the correct size.

import com.shakelang.util.primitives.bytes.toShort

val bytes = byteArrayOf(0x01, 0x02)
val short = bytes.toShort()

Works with all target types

See all functions
  • toByte() - Converts the data to a Byte: [0x01] >> 0x01

    val bytes = byteArrayOf(0x01)
    val byte = bytes.toByte() // >> 0x01
  • toShortBE() - Converts the data to a Short (big endian): [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02)
    val short = bytes.toShortBE() // >> 0x0102
  • toShortLE() - Converts the data to a Short (little endian): [0x01, 0x02] >> 0x0201

    val bytes = byteArrayOf(0x01, 0x02)
    val short = bytes.toShortLE() // >> 0x0201
  • toShort() - Converts the data to a Short (big endian): [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02)
    val short = bytes.toShort() // >> 0x0102
  • toIntBE() - Converts the data to a Int (big endian): [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val int = bytes.toIntBE() // >> 0x01020304
  • toIntLE() - Converts the data to a Int (little endian): [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val int = bytes.toIntLE() // >> 0x04030201
  • toInt() - Converts the data to a Int (big endian): [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val int = bytes.toInt() // >> 0x01020304
  • toLongBE() - Converts the data to a Long (big endian): [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val long = bytes.toLongBE() // >> 0x0102030405060708
  • toLongLE() - Converts the data to a Long (little endian): [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val long = bytes.toLongLE() // >> 0x0807060504030201
  • toLong() - Converts the data to a Long (big endian): [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val long = bytes.toLong() // >> 0x0102030405060708
  • toFloatBE() - Converts the data to a Float (big endian): [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00)
    val float = bytes.toFloatBE() // >> 1.0
  • toFloatLE() - Converts the data to a Float (little endian): [0x00, 0x00, 0x80, 0x3F] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x80, 0x3F)
    val float = bytes.toFloatLE() // >> 1.0
  • toFloat() - Converts the data to a Float (big endian): [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00)
    val float = bytes.toFloat() // >> 1.0
  • toDoubleBE() - Converts the data to a Double (big endian): [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    val double = bytes.toDoubleBE() // >> 1.0
  • toDoubleLE() - Converts the data to a Double (little endian): [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F)
    val double = bytes.toDoubleLE() // >> 1.0
  • toDouble() - Converts the data to a Double (big endian): [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    val double = bytes.toDouble() // >> 1.0
  • toUByte() - Converts the data to a UByte: [0x01] >> 0x01

    val bytes = byteArrayOf(0x01)
    val byte = bytes.toUByte() // >> 0x01
  • toUShortBE() - Converts the data to a UShort (big endian): [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02)
    val short = bytes.toUShortBE() // >> 0x0102
  • toUShortLE() - Converts the data to a UShort (little endian): [0x01, 0x02] >> 0x0201

    val bytes = byteArrayOf(0x01, 0x02)
    val short = bytes.toUShortLE() // >> 0x0201
  • toUShort() - Converts the data to a UShort (big endian): [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02)
    val short = bytes.toUShort() // >> 0x0102
  • toUIntBE() - Converts the data to a UInt (big endian): [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.toUIntBE() // >> 0x01020304
  • toUIntLE() - Converts the data to a UInt (little endian): [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.toUIntLE() // >> 0x04030201
  • toUInt() - Converts the data to a UInt (big endian): [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.toUInt() // >> 0x01020304
  • toULongBE() - Converts the data to a ULong (big endian): [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val short = bytes.toULongBE() // >> 0x0102030405060708
  • toULongLE() - Converts the data to a ULong (little endian): [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val short = bytes.toULongLE() // >> 0x0807060504030201
  • toULong() - Converts the data to a ULong (big endian): [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val short = bytes.toULong() // >> 0x0102030405060708

get

The get function gets a value from the data at a specific index.

import com.shakelang.util.primitives.bytes.getShort

val bytes = byteArrayOf(0x01, 0x02)
val short = bytes.getShort(0)
import com.shakelang.util.primitives.bytes.getShort

val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)

val short1 = bytes.getShort(1) // >> 0x0203
val short2 = bytes.getShort(0) // >> 0x0102

Works with all target types

See all functions
  • getByte(index: Int) - Gets a Byte at the index: [0x01] >> 0x01

    val bytes = byteArrayOf(0x01, 0x02)
    val byte = bytes.getByte(0) // >> 0x01
    val byte = bytes.getByte(1) // >> 0x02
  • getShortBE(index: Int) - Gets a Short (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.getShortBE(0) // >> 0x0102
    val short = bytes.getShortBE(1) // >> 0x0203
  • getShortLE(index: Int) - Gets a Short (little endian) at the index: [0x01, 0x02] >> 0x0201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.getShortLE(0) // >> 0x0201
    val short = bytes.getShortLE(1) // >> 0x0302
  • getShort(index: Int) - Gets a Short (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.getShort(0) // >> 0x0102
    val short = bytes.getShort(1) // >> 0x0203
  • getIntBE(index: Int) - Gets a Int (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val int = bytes.getIntBE(0) // >> 0x01020304
    val int = bytes.getIntBE(1) // >> 0x02030405
  • getIntLE(index: Int) - Gets a Int (little endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val int = bytes.getIntLE(0) // >> 0x04030201
    val int = bytes.getIntLE(1) // >> 0x05040302
  • getInt(index: Int) - Gets a Int (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val int = bytes.getInt(0) // >> 0x01020304
    val int = bytes.getInt(1) // >> 0x02030405
  • getLongBE(index: Int) - Gets a Long (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    val long = bytes.getLongBE(0) // >> 0x0102030405060708
    val long = bytes.getLongBE(1) // >> 0x0203040506070809
  • getLongLE(index: Int) - Gets a Long (little endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    val long = bytes.getLongLE(0) // >> 0x0807060504030201
    val long = bytes.getLongLE(1) // >> 0x0908070605040302
  • getLong(index: Int) - Gets a Long (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    val long = bytes.getLong(0) // >> 0x0102030405060708
    val long = bytes.getLong(1) // >> 0x0203040506070809
  • getFloatBE(index: Int) - Gets a Float (big endian) at the index: [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    val float = bytes.getFloatBE(0) // >> 1.0
    val float = bytes.getFloatBE(1) // >> 0.0
  • getFloatLE(index: Int) - Gets a Float (little endian) at the index: [0x00, 0x00, 0x80, 0x3F] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00)
    val float = bytes.getFloatLE(0) // >> 1.0
    val float = bytes.getFloatLE(1) // >> 0.0
  • getFloat(index: Int) - Gets a Float (big endian) at the index: [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    val float = bytes.getFloat(0) // >> 1.0
    val float = bytes.getFloat(1) // >> 0.0
  • getDoubleBE(index: Int) - Gets a Double (big endian) at the index: [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    val double = bytes.getDoubleBE(0) // >> 1.0
    val double = bytes.getDoubleBE(1) // >> 0.0
  • getDoubleLE(index: Int) - Gets a Double (little endian) at the index: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F)
    val double = bytes.getDoubleLE(8) // >> 1.0
    val double = bytes.getDoubleLE(1) // >> 0.0
  • getDouble(index: Int) - Gets a Double (big endian) at the index: [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    val double = bytes.getDouble(0) // >> 1.0
    val double = bytes.getDouble(1) // >> 0.0
  • getUByte(index: Int) - Gets a UByte at the index: [0x01] >> 0x01

    val bytes = byteArrayOf(0x01, 0x02)
    val byte = bytes.getUByte(0) // >> 0x01
    val byte = bytes.getUByte(1) // >> 0x02
  • getUShortBE(index: Int) - Gets a UShort (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.getUShortBE(0) // >> 0x0102
    val short = bytes.getUShortBE(1) // >> 0x0203
  • getUShortLE(index: Int) - Gets a UShort (little endian) at the index: [0x01, 0x02] >> 0x0201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.getUShortLE(0) // >> 0x0201
    val short = bytes.getUShortLE(1) // >> 0x0302
  • getUShort(index: Int) - Gets a UShort (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    val short = bytes.getUShort(0) // >> 0x0102
    val short = bytes.getUShort(1) // >> 0x0203
  • getUIntBE(index: Int) - Gets a UInt (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val int = bytes.getUIntBE(0) // >> 0x01020304
    val int = bytes.getUIntBE(1) // >> 0x02030405
  • getUIntLE(index: Int) - Gets a UInt (little endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val int = bytes.getUIntLE(0) // >> 0x04030201
    val int = bytes.getUIntLE(1) // >> 0x05040302
  • getUInt(index: Int) - Gets a UInt (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    val int = bytes.getUInt(0) // >> 0x01020304
    val int = bytes.getUInt(1) // >> 0x02030405
  • getULongBE(index: Int) - Gets a ULong (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    val long = bytes.getULongBE(0) // >> 0x0102030405060708
    val long = bytes.getULongBE(1) // >> 0x0203040506070809
  • getULongLE(index: Int) - Gets a ULong (little endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    val long = bytes.getULongLE(0) // >> 0x0807060504030201
    val long = bytes.getULongLE(1) // >> 0x0908070605040302
  • getULong(index: Int) - Gets a ULong (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    val long = bytes.getULong(0) // >> 0x0102030405060708
    val long = bytes.getULong(1) // >> 0x0203040506070809

set

The set function sets a value at a specific index. This function is not available on List<Byte> as it is immutable, but it is available on MutableList<Byte>.

import com.shakelang.util.primitives.bytes.setShort

val bytes = byteArrayOf(0x01, 0x02)
bytes.setShort(0, 0x0304)
import com.shakelang.util.primitives.bytes.setShort

val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)

bytes.setShort(1, 0x0506) // >> [0x01, 0x05, 0x06, 0x04]

Works with all target types

See all functions
  • setByte(index: Int, value: Byte) - Sets a Byte at the index: [0x01] >> 0x01

    val bytes = byteArrayOf(0x01, 0x02)
    bytes.setByte(0, 0x03) // >> [0x03, 0x02]
  • setShortBE(index: Int, value: Short) - Sets a Short (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    bytes.setShortBE(0, 0x0506) // >> [0x05, 0x06, 0x03, 0x04]
  • setShortLE(index: Int, value: Short) - Sets a Short (little endian) at the index: [0x01, 0x02] >> 0x0201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    bytes.setShortLE(0, 0x0506) // >> [0x06, 0x05, 0x03, 0x04]
  • setShort(index: Int, value: Short) - Sets a Short (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    bytes.setShort(0, 0x0506) // >> [0x05, 0x06, 0x03, 0x04]
  • setIntBE(index: Int, value: Int) - Sets a Int (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.setIntBE(0, 0x090A0B0C) // >> [0x09, 0x0A, 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08]
  • setIntLE(index: Int, value: Int) - Sets a Int (little endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.setIntLE(0, 0x090A0B0C) // >> [0x0C, 0x0B, 0x0A, 0x09, 0x05, 0x06, 0x07, 0x08]
  • setInt(index: Int, value: Int) - Sets a Int (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.setInt(0, 0x090A0B0C) // >> [0x09, 0x0A, 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08]
  • setLongBE(index: Int, value: Long) - Sets a Long (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.setLongBE(0, 0x1112131415161718) // >> [0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
  • setLongLE(index: Int, value: Long) - Sets a Long (little endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.setLongLE(0, 0x1112131415161718) // >> [0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
  • setLong(index: Int, value: Long) - Sets a Long (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.setLong(0, 0x1112131415161718) // >> [0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
  • setFloatBE(index: Int, value: Float) - Sets a Float (big endian) at the index: [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.setFloatBE(0, 1.0f) // >> [0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • setFloatLE(index: Int, value: Float) - Sets a Float (little endian) at the index: [0x00, 0x00, 0x80, 0x3F] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00)
    bytes.setFloatLE(0, 1.0f) // >> [0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • setFloat(index: Int, value: Float) - Sets a Float (big endian) at the index: [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.setFloat(0, 1.0f) // >> [0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • setDoubleBE(index: Int, value: Double) - Sets a Double (big endian) at the index: [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.setDoubleBE(0, 1.0) // >> [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • setDoubleLE(index: Int, value: Double) - Sets a Double (little endian) at the index: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.setDoubleLE(0, 1.0) // >> [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • setDouble(index: Int, value: Double) - Sets a Double (big endian) at the index: [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = byteArrayOf(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.setDouble(0, 1.0) // >> [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • setUByte(index: Int, value: UByte) - Sets a UByte at the index: [0x01] >> 0x01

    val bytes = byteArrayOf(0x01, 0x02)
    bytes.setUByte(0, 0x03) // >> [0x03, 0x02]
  • setUShortBE(index: Int, value: UShort) - Sets a UShort (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    bytes.setUShortBE(0, 0x0506) // >> [0x05, 0x06, 0x03, 0x04]
  • setUShortLE(index: Int, value: UShort) - Sets a UShort (little endian) at the index: [0x01, 0x02] >> 0x0201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    bytes.setUShortLE(0, 0x0506) // >> [0x06, 0x05, 0x03, 0x04]
  • setUShort(index: Int, value: UShort) - Sets a UShort (big endian) at the index: [0x01, 0x02] >> 0x0102

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04)
    bytes.setUShort(0, 0x0506) // >> [0x05, 0x06, 0x03, 0x04]
  • setUIntBE(index: Int, value: UInt) - Sets a UInt (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.setUIntBE(0, 0x090A0B0C) // >> [0x09, 0x0A, 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08]
  • setUIntLE(index: Int, value: UInt) - Sets a UInt (little endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.setUIntLE(0, 0x090A0B0C) // >> [0x0C, 0x0B, 0x0A, 0x09, 0x05, 0x06, 0x07, 0x08]
  • setUInt(index: Int, value: UInt) - Sets a UInt (big endian) at the index: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.setUInt(0, 0x090A0B0C) // >> [0x09, 0x0A, 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08]
  • setULongBE(index: Int, value: ULong) - Sets a ULong (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.setULongBE(0, 0x1112131415161718) // >> [0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
  • setULongLE(index: Int, value: ULong) - Sets a ULong (little endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.setULongLE(0, 0x1112131415161718) // >> [0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
  • setULong(index: Int, value: ULong) - Sets a ULong (big endian) at the index: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.setULong(0, 0x1112131415161718) // >> [0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]

append

This only works on MutableList<Byte> (array size is fixed, List<Byte> is immutable).

The append function appends a value to the end of the list.

import com.shakelang.util.primitives.bytes.appendShort

val bytes = mutableListOf<Byte>(0x01, 0x02)
bytes.appendShort(0x0304) // >> [0x01, 0x02, 0x03, 0x04]

Works with all target types

See all functions
  • appendByte(value: Byte) - Appends a Byte to the list: [0x01] >> 0x01

    val bytes = mutableListOf<Byte>(0x01, 0x02)
    bytes.appendByte(0x03) // >> [0x01, 0x02, 0x03]
  • appendShortBE(value: Short) - Appends a Short (big endian) to the list: [0x01, 0x02] >> 0x0102

    val bytes = mutableListOf<Byte>(0x01, 0x02)
    bytes.appendShortBE(0x0304) // >> [0x01, 0x02, 0x03, 0x04]
  • appendShortLE(value: Short) - Appends a Short (little endian) to the list: [0x01, 0x02] >> 0x0201

    val bytes = mutableListOf<Byte>(0x01, 0x02)
    bytes.appendShortLE(0x0304) // >> [0x01, 0x02, 0x04, 0x03]
  • appendShort(value: Short) - Appends a Short (big endian) to the list: [0x01, 0x02] >> 0x0102

    val bytes = mutableListOf<Byte>(0x01, 0x02)
    bytes.appendShort(0x0304) // >> [0x01, 0x02, 0x03, 0x04]
  • appendIntBE(value: Int) - Appends a Int (big endian) to the list: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04)
    bytes.appendIntBE(0x05060708) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
  • appendIntLE(value: Int) - Appends a Int (little endian) to the list: [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04)
    bytes.appendIntLE(0x05060708) // >> [0x01, 0x02, 0x03, 0x04, 0x08, 0x07, 0x06, 0x05]
  • appendInt(value: Int) - Appends a Int (big endian) to the list: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04)
    bytes.appendInt(0x05060708) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
  • appendLongBE(value: Long) - Appends a Long (big endian) to the list: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.appendLongBE(0x090A0B0C0D0E0F10) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
  • appendLongLE(value: Long) - Appends a Long (little endian) to the list: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.appendLongLE(0x090A0B0C0D0E0F10) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09]
  • appendLong(value: Long) - Appends a Long (big endian) to the list: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.appendLong(0x090A0B0C0D0E0F10) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
  • appendFloatBE(value: Float) - Appends a Float (big endian) to the list: [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = mutableListOf<Byte>(0x3F, 0x80, 0x00, 0x00)
    bytes.appendFloatBE(1.0f) // >> [0x3F, 0x80, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00]
  • appendFloatLE(value: Float) - Appends a Float (little endian) to the list: [0x00, 0x00, 0x80, 0x3F] >> 1.0

    val bytes = mutableListOf<Byte>(0x00, 0x00, 0x80, 0x3F)
    bytes.appendFloatLE(1.0f) // >> [0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x80, 0x3F]
  • appendFloat(value: Float) - Appends a Float (big endian) to the list: [0x3F, 0x80, 0x00, 0x00] >> 1.0

    val bytes = mutableListOf<Byte>(0x3F, 0x80, 0x00, 0x00)
    bytes.appendFloat(1.0f) // >> [0x3F, 0x80, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00]
  • appendDoubleBE(value: Double) - Appends a Double (big endian) to the list: [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = mutableListOf<Byte>(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.appendDoubleBE(1.0) // >> [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • appendDoubleLE(value: Double) - Appends a Double (little endian) to the list: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F] >> 1.0

    val bytes = mutableListOf<Byte>(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.appendDoubleLE(1.0) // >> [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F]
  • appendDouble(value: Double) - Appends a Double (big endian) to the list: [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] >> 1.0

    val bytes = mutableListOf<Byte>(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    bytes.appendDouble(1.0) // >> [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • appendUByte(value: UByte) - Appends a UByte to the list: [0x01] >> 0x01

    val bytes = mutableListOf<Byte>(0x01, 0x02)
    bytes.appendUByte(0x03) // >> [0x01, 0x02, 0x03]
  • appendUShortBE(value: UShort) - Appends a UShort (big endian) to the list: [0x01, 0x02] >> 0x0102

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04)
    bytes.appendUShortBE(0x0506) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]
  • appendUShortLE(value: UShort) - Appends a UShort (little endian) to the list: [0x01, 0x02] >> 0x0201

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04)
    bytes.appendUShortLE(0x0506) // >> [0x01, 0x02, 0x03, 0x04, 0x06, 0x05]
  • appendUShort(value: UShort) - Appends a UShort (big endian) to the list: [0x01, 0x02] >> 0x0102

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04)
    bytes.appendUShort(0x0506) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]
  • appendUIntBE(value: UInt) - Appends a UInt (big endian) to the list: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.appendUIntBE(0x090A0B0C) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C]
  • appendUIntLE(value: UInt) - Appends a UInt (little endian) to the list: [0x01, 0x02, 0x03, 0x04] >> 0x04030201

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.appendUIntLE(0x090A0B0C) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0C, 0x0B, 0x0A, 0x09]
  • appendUInt(value: UInt) - Appends a UInt (big endian) to the list: [0x01, 0x02, 0x03, 0x04] >> 0x01020304

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
    bytes.appendUInt(0x090A0B0C) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C]
  • appendULongBE(value: ULong) - Appends a ULong (big endian) to the list: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.appendULongBE(0x1112131415161718) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18]
  • appendULongLE(value: ULong) - Appends a ULong (little endian) to the list: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0807060504030201

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.appendULongLE(0x1112131415161718) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11]
  • appendULong(value: ULong) - Appends a ULong (big endian) to the list: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] >> 0x0102030405060708

    val bytes = mutableListOf<Byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10)
    bytes.appendULong(0x1112131415161718) // >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18]

Conversion to bytes

from

To convert the data back to byte arrays, you can just apply toBytes() to the data.

import com.shakelang.util.primitives.bytes.toBytes

val bytes = 0x0102.toBytes() // >> [0x01, 0x02]

For Short, Int, Long, Float, Double, USort, UInt, ULong you can also use the toBytesBE() and toBytesLE() functions.

import com.shakelang.util.primitives.bytes.toBytesBE

val bytes = 0x0102.toBytesBE() // >> [0x01, 0x02]
import com.shakelang.util.primitives.bytes.toBytesLE

val bytes = 0x0102.toBytesLE() // >> [0x02, 0x01]

(For Byte and UByte this is not necessary as there is only one byte, so there is no point in having a big endian and little endian version)

Works with all target types

See all functions
  • Byte.toBytes() - Converts a Byte to a byte array: 0x01 >> [0x01]
  • Short.toBytesBE() - Converts a Short (big endian) to a byte array: 0x0102 >> [0x01, 0x02]
  • Short.toBytesLE() - Converts a Short (little endian) to a byte array: 0x0102 >> [0x02, 0x01]
  • Short.toBytes() - Converts a Short (big endian) to a byte array: 0x0102 >> [0x01, 0x02]
  • Int.toBytesBE() - Converts a Int (big endian) to a byte array: 0x01020304 >> [0x01, 0x02, 0x03, 0x04]
  • Int.toBytesLE() - Converts a Int (little endian) to a byte array: 0x01020304 >> [0x04, 0x03, 0x02, 0x01]
  • Int.toBytes() - Converts a Int (big endian) to a byte array: 0x01020304 >> [0x01, 0x02, 0x03, 0x04]
  • Long.toBytesBE() - Converts a Long (big endian) to a byte array: 0x0102030405060708 >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
  • Long.toBytesLE() - Converts a Long (little endian) to a byte array: 0x0102030405060708 >> [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01]
  • Long.toBytes() - Converts a Long (big endian) to a byte array: 0x0102030405060708 >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
  • Float.toBytesBE() - Converts a Float (big endian) to a byte array: 1.0 >> [0x3F, 0x80, 0x00, 0x00]
  • Float.toBytesLE() - Converts a Float (little endian) to a byte array: 1.0 >> [0x00, 0x00, 0x80, 0x3F]
  • Float.toBytes() - Converts a Float (big endian) to a byte array: 1.0 >> [0x3F, 0x80, 0x00, 0x00]
  • Double.toBytesBE() - Converts a Double (big endian) to a byte array: 1.0 >> [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • Double.toBytesLE() - Converts a Double (little endian) to a byte array: 1.0 >> [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F]
  • Double.toBytes() - Converts a Double (big endian) to a byte array: 1.0 >> [0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  • UByte.toBytes() - Converts a UByte to a byte array: 0x01 >> [0x01]
  • UShort.toBytesBE() - Converts a UShort (big endian) to a byte array: 0x0102 >> [0x01, 0x02]
  • UShort.toBytesLE() - Converts a UShort (little endian) to a byte array: 0x0102 >> [0x02, 0x01]
  • UShort.toBytes() - Converts a UShort (big endian) to a byte array: 0x0102 >> [0x01, 0x02]
  • UInt.toBytesBE() - Converts a UInt (big endian) to a byte array: 0x01020304 >> [0x01, 0x02, 0x03, 0x04]
  • UInt.toBytesLE() - Converts a UInt (little endian) to a byte array: 0x01020304 >> [0x04, 0x03, 0x02, 0x01]
  • UInt.toBytes() - Converts a UInt (big endian) to a byte array: 0x01020304 >> [0x01, 0x02, 0x03, 0x04]
  • ULong.toBytesBE() - Converts a ULong (big endian) to a byte array: 0x0102030405060708 >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
  • ULong.toBytesLE() - Converts a ULong (little endian) to a byte array: 0x0102030405060708 >> [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01]
  • ULong.toBytes() - Converts a ULong (big endian) to a byte array: 0x0102030405060708 >> [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]

of

This set of functions creates data from raw bytes.

  • byteOf(b0: Byte) - Creates a Byte from a byte: 0x01 >> 0x01
  • shortOf(b0: Byte, b1: Byte) - Creates a Short from two bytes: 0x01, 0x02 >> 0x0102
  • intOf(b0: Byte, b1: Byte, b2: Byte, b3: Byte) - Creates a Int from four bytes: 0x01, 0x02, 0x03, 0x04 >> 0x01020304
  • longOf(b0: Byte, b1: Byte, b2: Byte, b3: Byte, b4: Byte, b5: Byte, b6: Byte, b7: Byte) - Creates a Long from eight bytes: 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 >> 0x0102030405060708
  • floatOf(b0: Byte, b1: Byte, b2: Byte, b3: Byte) - Creates a Float from four bytes: 0x3F, 0x80, 0x00, 0x00 >> 1.0
  • doubleOf(b0: Byte, b1: Byte, b2: Byte, b3: Byte, b4: Byte, b5: Byte, b6: Byte, b7: Byte) - Creates a Double from eight bytes: 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 >> 1.0
  • ubyteOf(b0: Byte) - Creates a UByte from a byte: 0x01 >> 0x01
  • ushortOf(b0: Byte, b1: Byte) - Creates a UShort from two bytes: 0x01, 0x02 >> 0x0102
  • uintOf(b0: Byte, b1: Byte, b2: Byte, b3: Byte) - Creates a UInt from four bytes: 0x01, 0x02, 0x03, 0x04 >> 0x01020304
  • ulongOf(b0: Byte, b1: Byte, b2: Byte, b3: Byte, b4: Byte, b5: Byte, b6: Byte, b7: Byte) - Creates a ULong from eight bytes: 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 >> 0x0102030405060708
import com.shakelang.util.primitives.bytes.byteOf
val bytes = byteOf(0x01)
import com.shakelang.util.primitives.bytes.shortOf
val bytes = shortOf(0x01, 0x02)
import com.shakelang.util.primitives.bytes.intOf
val bytes = intOf(0x01, 0x02, 0x03, 0x04)
import com.shakelang.util.primitives.bytes.longOf
val bytes = longOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
import com.shakelang.util.primitives.bytes.floatOf
val bytes = floatOf(0x3F, 0x80, 0x00, 0x00)
import com.shakelang.util.primitives.bytes.doubleOf
val bytes = doubleOf(0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
import com.shakelang.util.primitives.bytes.ubyteOf
val bytes = ubyteOf(0x01)
import com.shakelang.util.primitives.bytes.ushortOf
val bytes = ushortOf(0x01, 0x02)
import com.shakelang.util.primitives.bytes.uintOf
val bytes = uintOf(0x01, 0x02, 0x03, 0x04)
import com.shakelang.util.primitives.bytes.ulongOf
val bytes = ulongOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)