decode int from bytes
This commit is contained in:
@@ -5,7 +5,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "me.alex_s168"
|
group = "me.alex_s168"
|
||||||
version = "0.2"
|
version = "0.3"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
@@ -1,5 +1,8 @@
|
|||||||
package blitz
|
package blitz
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer
|
||||||
|
import java.nio.ByteOrder
|
||||||
|
|
||||||
enum class Endian {
|
enum class Endian {
|
||||||
LITTLE,
|
LITTLE,
|
||||||
BIG
|
BIG
|
||||||
@@ -8,8 +11,15 @@ enum class Endian {
|
|||||||
infix fun encodeLittle(little: ByteArray) =
|
infix fun encodeLittle(little: ByteArray) =
|
||||||
if (this == BIG) little.reversedArray()
|
if (this == BIG) little.reversedArray()
|
||||||
else little
|
else little
|
||||||
|
|
||||||
|
fun toNIO(): ByteOrder =
|
||||||
|
if (this == LITTLE) ByteOrder.LITTLE_ENDIAN
|
||||||
|
else ByteOrder.BIG_ENDIAN
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ByteBuffer.order(endian: Endian): ByteBuffer =
|
||||||
|
order(endian.toNIO())
|
||||||
|
|
||||||
fun Long.toBytes(endian: Endian) =
|
fun Long.toBytes(endian: Endian) =
|
||||||
endian encodeLittle
|
endian encodeLittle
|
||||||
toInt().toBytes(Endian.LITTLE) +
|
toInt().toBytes(Endian.LITTLE) +
|
||||||
@@ -36,4 +46,30 @@ fun Byte.toBytes() =
|
|||||||
byteArrayOf(this)
|
byteArrayOf(this)
|
||||||
|
|
||||||
fun UByte.toBytes() =
|
fun UByte.toBytes() =
|
||||||
toByte().toBytes()
|
toByte().toBytes()
|
||||||
|
|
||||||
|
// TODO: no cheat
|
||||||
|
|
||||||
|
fun ByteArray.toShort(endian: Endian) =
|
||||||
|
ByteBuffer.wrap(this).order(endian).getShort()
|
||||||
|
|
||||||
|
fun ByteArray.toUShort(endian: Endian) =
|
||||||
|
toShort(endian).toUShort()
|
||||||
|
|
||||||
|
fun ByteArray.toInt(endian: Endian) =
|
||||||
|
ByteBuffer.wrap(this).order(endian).getInt()
|
||||||
|
|
||||||
|
fun ByteArray.toUInt(endian: Endian) =
|
||||||
|
toInt(endian).toUInt()
|
||||||
|
|
||||||
|
fun ByteArray.toLong(endian: Endian) =
|
||||||
|
ByteBuffer.wrap(this).order(endian).getLong()
|
||||||
|
|
||||||
|
fun ByteArray.toULong(endian: Endian) =
|
||||||
|
toLong(endian).toULong()
|
||||||
|
|
||||||
|
fun ByteArray.toByte() =
|
||||||
|
this[0]
|
||||||
|
|
||||||
|
fun ByteArray.toUByte() =
|
||||||
|
this[0].toUByte()
|
Reference in New Issue
Block a user