package com.infendro.bytes.intarray import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN fun IntArray.copyInto( destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN, ) { val length = endIndex - startIndex require(length >= 0) require(startIndex in 0..(size - length)) require(destinationOffset in 0..(destination.size - (length * 4))) when (order) { BIG_ENDIAN -> { repeat(length) { i -> val offset = destinationOffset + (i * 4) val value = this[startIndex + i] destination[offset] = (value shr 24).toByte() destination[offset + 1] = (value shr 16).toByte() destination[offset + 2] = (value shr 8).toByte() destination[offset + 3] = value.toByte() } } LITTLE_ENDIAN -> { repeat(length) { i -> val offset = destinationOffset + (i * 4) val value = this[startIndex + i] destination[offset] = value.toByte() destination[offset + 1] = (value shr 8).toByte() destination[offset + 2] = (value shr 16).toByte() destination[offset + 3] = (value shr 24).toByte() } } } } fun IntArray.copyInto( destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN, ) = copyInto(destination.asByteArray(), destinationOffset, startIndex, endIndex, order)