fix ByteArray to numeric conversion
All checks were successful
/ test (pull_request) Successful in 2m49s
All checks were successful
/ test (pull_request) Successful in 2m49s
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user