martmists
05/23/2023, 9:09 PMfun ByteArray.readInt(offset: Int, endianness: Endianness = Endianness.LITTLE): Int {
return when (endianness) {
Endianness.BIG -> {
((this[offset].toInt() shl 24) or
(this[offset + 1].toInt() shl 16) or
(this[offset + 2].toInt() shl 8) or
(this[offset + 3].toInt() shl 0))
}
Endianness.LITTLE -> {
((this[offset].toInt() shl 0) or
(this[offset + 1].toInt() shl 8) or
(this[offset + 2].toInt() shl 16) or
(this[offset + 3].toInt() shl 24))
}
}.also {
println("Read int $it from bytes ${this[offset]}, ${this[offset + 1]}, ${this[offset + 2]} and ${this[offset + 3]}")
}
}
as it outputs
Read int -38 from bytes -38, 12, 0 and 0
but it should output
Read int 3290 from bytes -38, 12, 0 and 0
where the input data is [0xDA, 0x0C, 0x00, 0x00]
read from a file using JVM File().readBytes()
Joel Denke
05/24/2023, 5:23 AMJiri Bruchanov
05/24/2023, 6:33 AMMichael Paus
05/24/2023, 8:16 AMtgtImage[refIndex] = (
((0xFF and tgtA) shl 24) or
((0xFF and tgtR) shl 16) or
((0xFF and tgtG) shl 8) or
((0xFF and tgtB) shl 0)
)