diff --git a/README.md b/README.md index 321fc3f..cbdda32 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,6 @@ repositories { } commonMain.dependencies { - implementation("com.infendro:bytes:1.3.1") + implementation("com.infendro:bytes:1.3.2") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 0746eef..eadd64b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ import com.infendro.plugin.token import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl group = "com.infendro" -version = "1.3.1" +version = "1.3.2" plugins { alias(libs.plugins.infendro) diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt index 458357c..377a32d 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt @@ -9,12 +9,16 @@ fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int { return when (order) { BIG_ENDIAN -> { - (this[startIndex].toInt() shl 24) or (this[startIndex + 1].toInt() shl 16) or - (this[startIndex + 2].toInt() shl 8) or this[startIndex + 3].toInt() + ((this[startIndex].toInt() and 0xFF) shl 24) or + ((this[startIndex + 1].toInt() and 0xFF) shl 16) or + ((this[startIndex + 2].toInt() and 0xFF) shl 8) or + (this[startIndex + 3].toInt() and 0xFF) } LITTLE_ENDIAN -> { - this[startIndex].toInt() or (this[startIndex + 1].toInt() shl 8) or - (this[startIndex + 2].toInt() shl 16) or (this[startIndex + 3].toInt() shl 24) + (this[startIndex].toInt() and 0xFF) or + ((this[startIndex + 1].toInt() and 0xFF) shl 8) or + ((this[startIndex + 2].toInt() and 0xFF) shl 16) or + ((this[startIndex + 3].toInt() and 0xFF) shl 24) } } } @@ -33,16 +37,24 @@ fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long { return when (order) { BIG_ENDIAN -> { - (this[startIndex].toLong() shl 56) or (this[startIndex + 1].toLong() shl 48) or - (this[startIndex + 2].toLong() shl 40) or (this[startIndex + 3].toLong() shl 32) or - (this[startIndex + 4].toLong() shl 24) or (this[startIndex + 5].toLong() shl 16) or - (this[startIndex + 6].toLong() shl 8) or this[startIndex + 7].toLong() + ((this[startIndex].toLong() and 0xFFL) shl 56) or + ((this[startIndex + 1].toLong() and 0xFFL) shl 48) or + ((this[startIndex + 2].toLong() and 0xFFL) shl 40) or + ((this[startIndex + 3].toLong() and 0xFFL) shl 32) or + ((this[startIndex + 4].toLong() and 0xFFL) shl 24) or + ((this[startIndex + 5].toLong() and 0xFFL) shl 16) or + ((this[startIndex + 6].toLong() and 0xFFL) shl 8) or + (this[startIndex + 7].toLong() and 0xFFL) } LITTLE_ENDIAN -> { - this[startIndex].toLong() or (this[startIndex + 1].toLong() shl 8) or - (this[startIndex + 2].toLong() shl 16) or (this[startIndex + 3].toLong() shl 24) or - (this[startIndex + 4].toLong() shl 32) or (this[startIndex + 5].toLong() shl 40) or - (this[startIndex + 6].toLong() shl 48) or (this[startIndex + 7].toLong() shl 56) + (this[startIndex].toLong() and 0xFFL) or + ((this[startIndex + 1].toLong() and 0xFFL) shl 8) or + ((this[startIndex + 2].toLong() and 0xFFL) shl 16) or + ((this[startIndex + 3].toLong() and 0xFFL) shl 24) or + ((this[startIndex + 4].toLong() and 0xFFL) shl 32) or + ((this[startIndex + 5].toLong() and 0xFFL) shl 40) or + ((this[startIndex + 6].toLong() and 0xFFL) shl 48) or + ((this[startIndex + 7].toLong() and 0xFFL) shl 56) } } } diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt index 7deb708..b5ce9f9 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt @@ -21,16 +21,20 @@ fun ByteArray.copyInto( repeat(length / 4) { i -> val offset = startIndex + (i * 4) destination[destinationOffset + i] = - (this[offset].toInt() shl 24) or (this[offset + 1].toInt() shl 16) or - (this[offset + 2].toInt() shl 8) or this[offset + 3].toInt() + ((this[offset].toInt() and 0xFF) shl 24) or + ((this[offset + 1].toInt() and 0xFF) shl 16) or + ((this[offset + 2].toInt() and 0xFF) shl 8) or + (this[offset + 3].toInt() and 0xFF) } } LITTLE_ENDIAN -> { repeat(length / 4) { i -> val offset = startIndex + (i * 4) destination[destinationOffset + i] = - this[offset].toInt() or (this[offset + 1].toInt() shl 8) or - (this[offset + 2].toInt() shl 16) or (this[offset + 3].toInt() shl 24) + (this[offset].toInt() and 0xFF) or + ((this[offset + 1].toInt() and 0xFF) shl 8) or + ((this[offset + 2].toInt() and 0xFF) shl 16) or + ((this[offset + 3].toInt() and 0xFF) shl 24) } } } @@ -61,20 +65,28 @@ fun ByteArray.copyInto( repeat(length / 8) { i -> val offset = startIndex + (i * 8) destination[destinationOffset + i] = - (this[offset].toLong() shl 56) or (this[offset + 1].toLong() shl 48) or - (this[offset + 2].toLong() shl 40) or (this[offset + 3].toLong() shl 32) or - (this[offset + 4].toLong() shl 24) or (this[offset + 5].toLong() shl 16) or - (this[offset + 6].toLong() shl 8) or this[offset + 7].toLong() + ((this[offset].toLong() and 0xFFL) shl 56) or + ((this[offset + 1].toLong() and 0xFFL) shl 48) or + ((this[offset + 2].toLong() and 0xFFL) shl 40) or + ((this[offset + 3].toLong() and 0xFFL) shl 32) or + ((this[offset + 4].toLong() and 0xFFL) shl 24) or + ((this[offset + 5].toLong() and 0xFFL) shl 16) or + ((this[offset + 6].toLong() and 0xFFL) shl 8) or + (this[offset + 7].toLong() and 0xFFL) } } LITTLE_ENDIAN -> { repeat(length / 8) { i -> val offset = startIndex + (i * 8) destination[destinationOffset + i] = - this[offset].toLong() or (this[offset + 1].toLong() shl 8) or - (this[offset + 2].toLong() shl 16) or (this[offset + 3].toLong() shl 24) or - (this[offset + 4].toLong() shl 32) or (this[offset + 5].toLong() shl 40) or - (this[offset + 6].toLong() shl 48) or (this[offset + 7].toLong() shl 56) + (this[offset].toLong() and 0xFFL) or + ((this[offset + 1].toLong() and 0xFFL) shl 8) or + ((this[offset + 2].toLong() and 0xFFL) shl 16) or + ((this[offset + 3].toLong() and 0xFFL) shl 24) or + ((this[offset + 4].toLong() and 0xFFL) shl 32) or + ((this[offset + 5].toLong() and 0xFFL) shl 40) or + ((this[offset + 6].toLong() and 0xFFL) shl 48) or + ((this[offset + 7].toLong() and 0xFFL) shl 56) } } }