pack

fun pack(source: Short, dest: ByteArray, destOffset: Int): ByteArray(source)

Packs 2 bytes from source Short into dest ByteArray, starting at destOffset.

e.g.

val source = 2702.toShort()
val dest = ByteArray(2) { -100 }
println(dest.toList())
// [-100, -100]

Endian.Big.pack(source, dest, 0)
println(dest.toList())
// [10, -114]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
println(dest.toList())
// [-114, 10]

Return

The dest array

Parameters

source

The Short to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: Short, dest: ByteArray, destOffset: Int, sourceIndexStart: Int, sourceIndexEnd: Int = Short.SIZE_BYTES): ByteArray(source)

Packs bytes from source Short (max 2 bytes) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest ByteArray, starting at destOffset.

e.g.

val source = 2702.toShort()
val dest = ByteArray(2) { -100 }
println(dest.toList())
// [-100, -100]

Endian.Big.pack(source, dest, 0)
println(dest.toList())
// [10, -114]
dest.fill(-100)

Endian.Big.pack(source, dest, 1, sourceIndexStart = 0, sourceIndexEnd = 1)
println(dest.toList())
// [-100, 10]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
println(dest.toList())
// [-114, 10]
dest.fill(-100)

Endian.Little.pack(source, dest, 1, sourceIndexStart = 1)
println(dest.toList())
// [-100, 10]

Return

The dest array

Parameters

source

The Short to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source byte subrange.

sourceIndexEnd

The end (exclusive) of the source byte subrange, Short.SIZE_BYTES by default.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: ShortArray, dest: ByteArray, destOffset: Int, sourceIndexStart: Int = 0, sourceIndexEnd: Int = source.size): ByteArray(source)

Packs bytes from source ShortArray (max size * 2 bytes) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest ByteArray, starting at destOffset.

e.g.

val source = ShortArray(3) { i -> (i + 2701).toShort() }
val dest = ByteArray(source.size * Short.SIZE_BYTES) { -100 }
println(dest.toList())
println(source.toList())
// [-100, -100, -100, -100, -100, -100]
// [2701, 2702, 2703]

Endian.Big.pack(source, dest, 0)
source.fill(0)

Endian.Big.pack(source = dest, dest = source, 0, 0, sourceIndexEnd = dest.size)
println(dest.toList())
println(source.toList())
// [10, -115, 10, -114, 10, -113]
// [2701, 2702, 2703]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
source.fill(0)

Endian.Little.pack(source = dest, dest = source, 1, dest.size - 4, dest.size)
println(dest.toList())
println(source.toList())
// [-115, 10, -114, 10, -113, 10]
// [0, 2702, 2703]

Return

The dest array

Parameters

source

The ShortArray to retrieve Shorts from to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source subrange, 0 by default.

sourceIndexEnd

The end (exclusive) of the source subrange, source.size by default.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: ByteArray, dest: ShortArray, destOffset: Int, sourceIndexStart: Int, sourceIndexEnd: Int): ShortArray(source)

Packs Shorts from source ByteArray (max size / 2 Shorts) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest ShortArray, starting at destOffset.

e.g.

val source = ShortArray(3) { i -> (i + 2701).toShort() }
val dest = ByteArray(source.size * Short.SIZE_BYTES) { -100 }
println(dest.toList())
println(source.toList())
// [-100, -100, -100, -100, -100, -100]
// [2701, 2702, 2703]

Endian.Big.pack(source, dest, 0)
source.fill(0)

Endian.Big.pack(source = dest, dest = source, 0, 0, sourceIndexEnd = dest.size)
println(dest.toList())
println(source.toList())
// [10, -115, 10, -114, 10, -113]
// [2701, 2702, 2703]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
source.fill(0)

Endian.Little.pack(source = dest, dest = source, 1, dest.size - 4, dest.size)
println(dest.toList())
println(source.toList())
// [-115, 10, -114, 10, -113, 10]
// [0, 2702, 2703]

Return

The dest array

Parameters

source

The ByteArray to retrieve bytes from to convert into Shorts.

dest

The ShortArray to pack Shorts into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source subrange.

sourceIndexEnd

The end (exclusive) of the source subrange.

See also

Throws

IndexOutOfBoundsException

when subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: Int, dest: ByteArray, destOffset: Int): ByteArray(source)

Packs 4 bytes from source Int into dest ByteArray, starting at destOffset.

e.g.

val source = -77362181
val dest = ByteArray(4) { -100 }
println(dest.toList())
// [-100, -100, -100, -100]

Endian.Big.pack(source, dest, 0)
println(dest.toList())
// [-5, 99, -117, -5]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
println(dest.toList())
// [-5, -117, 99, -5]

Return

The dest array

Parameters

source

The Int to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: Int, dest: ByteArray, destOffset: Int, sourceIndexStart: Int, sourceIndexEnd: Int = Int.SIZE_BYTES): ByteArray(source)

Packs bytes from source Int (max 4 bytes) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest ByteArray, starting at destOffset.

e.g.

val source = -77362181
val dest = ByteArray(4) { -100 }
println(dest.toList())
// [-100, -100, -100, -100]

Endian.Big.pack(source, dest, 0)
println(dest.toList())
// [-5, 99, -117, -5]
dest.fill(-100)

Endian.Big.pack(source, dest, 2, sourceIndexStart = 1, sourceIndexEnd = 3)
println(dest.toList())
// [-100, -100, 99, -117]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
println(dest.toList())
// [-5, -117, 99, -5]
dest.fill(-100)

Endian.Little.pack(source, dest, 1, sourceIndexStart = 3)
println(dest.toList())
// [-100, -5, -100, -100]

Return

The dest array

Parameters

source

The Int to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source byte subrange.

sourceIndexEnd

The end (exclusive) of the source byte subrange, Int.SIZE_BYTES by default.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: IntArray, dest: ByteArray, destOffset: Int, sourceIndexStart: Int = 0, sourceIndexEnd: Int = source.size): ByteArray(source)

Packs bytes from source IntArray (max size * 4 bytes) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest ByteArray, starting at destOffset.

e.g.

val source = IntArray(2) { i -> i - 77362181 }
val dest = ByteArray(source.size * Int.SIZE_BYTES) { -100 }
println(dest.toList())
println(source.toList())
// [-100, -100, -100, -100, -100, -100, -100, -100]
// [-77362181, -77362180]

Endian.Big.pack(source, dest, 0)
source.fill(0)

Endian.Big.pack(source = dest, dest = source, 0, 0, sourceIndexEnd = dest.size)
println(dest.toList())
println(source.toList())
// [-5, 99, -117, -5, -5, 99, -117, -4]
// [-77362181, -77362180]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
source.fill(0)

Endian.Little.pack(source = dest, dest = source, 1, dest.size - 4, dest.size)
println(dest.toList())
println(source.toList())
// [-5, -117, 99, -5, -4, -117, 99, -5]
// [0, -77362180]

Return

The dest array

Parameters

source

The IntArray to retrieve Ints from to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source subrange, 0 by default.

sourceIndexEnd

The end (exclusive) of the source subrange, source.size by default.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: ByteArray, dest: IntArray, destOffset: Int, sourceIndexStart: Int, sourceIndexEnd: Int): IntArray(source)

Packs Ints from source ByteArray (max size / 4 Ints) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest IntArray, starting at destOffset.

e.g.

val source = IntArray(2) { i -> i - 77362181 }
val dest = ByteArray(source.size * Int.SIZE_BYTES) { -100 }
println(dest.toList())
println(source.toList())
// [-100, -100, -100, -100, -100, -100, -100, -100]
// [-77362181, -77362180]

Endian.Big.pack(source, dest, 0)
source.fill(0)

Endian.Big.pack(source = dest, dest = source, 0, 0, sourceIndexEnd = dest.size)
println(dest.toList())
println(source.toList())
// [-5, 99, -117, -5, -5, 99, -117, -4]
// [-77362181, -77362180]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
source.fill(0)

Endian.Little.pack(source = dest, dest = source, 1, dest.size - 4, dest.size)
println(dest.toList())
println(source.toList())
// [-5, -117, 99, -5, -4, -117, 99, -5]
// [0, -77362180]

Return

The dest array

Parameters

source

The ByteArray to retrieve bytes from to convert into Ints.

dest

The IntArray to pack Ints into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source subrange.

sourceIndexEnd

The end (exclusive) of the source subrange.

See also

Throws

IndexOutOfBoundsException

when subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: Long, dest: ByteArray, destOffset: Int): ByteArray(source)

Packs 8 bytes from source Long into dest ByteArray, starting at destOffset.

e.g.

val source = 9223372034707292160L
val dest = ByteArray(8) { -100 }
println(dest.toList())
// [-100, -100, -100, -100, -100, -100, -100, -100]

Endian.Big.pack(source, dest, 0)
println(dest.toList())
// [127, -1, -1, -1, -128, 0, 0, 0]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
println(dest.toList())
// [0, 0, 0, -128, -1, -1, -1, 127]

Return

The dest array

Parameters

source

The Long to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: Long, dest: ByteArray, destOffset: Int, sourceIndexStart: Int, sourceIndexEnd: Int = Long.SIZE_BYTES): ByteArray(source)

Packs bytes from source Long (max 8 bytes) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest ByteArray, starting at destOffset.

e.g.

val source = 9223372034707292160L
val dest = ByteArray(8) { -100 }
println(dest.toList())
// [-100, -100, -100, -100, -100, -100, -100, -100]

Endian.Big.pack(source, dest, 0)
println(dest.toList())
// [127, -1, -1, -1, -128, 0, 0, 0]
dest.fill(-100)

Endian.Big.pack(source, dest, 2, sourceIndexStart = 1, sourceIndexEnd = 7)
println(dest.toList())
// [-100, -100, -1, -1, -1, -128, 0, 0]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
println(dest.toList())
// [0, 0, 0, -128, -1, -1, -1, 127]
dest.fill(-100)

Endian.Little.pack(source, dest, 1, sourceIndexStart = 3)
println(dest.toList())
// [-100, -128, -1, -1, -1, 127, -100, -100]

Return

The dest array

Parameters

source

The Long to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source byte subrange.

sourceIndexEnd

The end (exclusive) of the source byte subrange, Long.SIZE_BYTES by default.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: LongArray, dest: ByteArray, destOffset: Int, sourceIndexStart: Int = 0, sourceIndexEnd: Int = source.size): ByteArray(source)

Packs bytes from source LongArray (max size * 8 bytes) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest ByteArray, starting at destOffset.

e.g.

val source = LongArray(2) { i -> i + 9223372034707292160L }
val dest = ByteArray(source.size * Long.SIZE_BYTES) { -100 }
println(dest.toList())
println(source.toList())
// [-100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100]
// [9223372034707292160, 9223372034707292161]

Endian.Big.pack(source, dest, 0)
source.fill(0L)

Endian.Big.pack(source = dest, dest = source, 0, 0, sourceIndexEnd = dest.size)
println(dest.toList())
println(source.toList())
// [127, -1, -1, -1, -128, 0, 0, 0, 127, -1, -1, -1, -128, 0, 0, 1]
// [9223372034707292160, 9223372034707292161]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
source.fill(0L)

Endian.Little.pack(source = dest, dest = source, 1, dest.size - 8, dest.size)
println(dest.toList())
println(source.toList())
// [0, 0, 0, -128, -1, -1, -1, 127, 1, 0, 0, -128, -1, -1, -1, 127]
// [0, 9223372034707292161]

Return

The dest array

Parameters

source

The LongArray to retrieve Longs from to convert into bytes.

dest

The ByteArray to pack bytes into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source subrange, 0 by default.

sourceIndexEnd

The end (exclusive) of the source subrange, source.size by default.

See also

Throws

IndexOutOfBoundsException

when byte subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.


fun pack(source: ByteArray, dest: LongArray, destOffset: Int, sourceIndexStart: Int, sourceIndexEnd: Int): LongArray(source)

Packs Longs from source ByteArray (max size / 8 Longs) from sourceIndexStart (inclusive) to sourceIndexEnd (exclusive) into dest LongArray, starting at destOffset.

e.g.

val source = LongArray(2) { i -> i + 9223372034707292160L }
val dest = ByteArray(source.size * Long.SIZE_BYTES) { -100 }
println(dest.toList())
println(source.toList())
// [-100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100]
// [9223372034707292160, 9223372034707292161]

Endian.Big.pack(source, dest, 0)
source.fill(0L)

Endian.Big.pack(source = dest, dest = source, 0, 0, sourceIndexEnd = dest.size)
println(dest.toList())
println(source.toList())
// [127, -1, -1, -1, -128, 0, 0, 0, 127, -1, -1, -1, -128, 0, 0, 1]
// [9223372034707292160, 9223372034707292161]
dest.fill(-100)

Endian.Little.pack(source, dest, 0)
source.fill(0L)

Endian.Little.pack(source = dest, dest = source, 1, dest.size - 8, dest.size)
println(dest.toList())
println(source.toList())
// [0, 0, 0, -128, -1, -1, -1, 127, 1, 0, 0, -128, -1, -1, -1, 127]
// [0, 9223372034707292161]

Return

The dest array

Parameters

source

The ByteArray to retrieve bytes from to convert into Longs.

dest

The LongArray to pack Longs into.

destOffset

The position in dest to start packing.

sourceIndexStart

The beginning (inclusive) of the source subrange.

sourceIndexEnd

The end (exclusive) of the source subrange.

See also

Throws

IndexOutOfBoundsException

when subrange will not fit into the dest array starting at the specified destOffset, or when that index is out of the dest array indices range.