some support for fixed number
This commit is contained in:
1 parent
5b8df2ff20
commit
86eb5e9d7f
8 files changed
+81
-15
No files matched your search
@@ -36,6 +36,10 @@ inline fun <reified T:Any>DataSink.writeNumber(value: T) {
|
||||
fun DataSink.writeI32(value: Int) {
|
||||
writeBytes(intToBytes(value))
|
||||
}
|
||||
|
||||
fun DataSink.writeI16(value: Short) {
|
||||
writeBytes(shortToBytes(value))
|
||||
}
|
||||
fun DataSink.writeU32(value: UInt) {
|
||||
writeBytes(uintToBytes(value))
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ interface DataSource {
|
||||
|
||||
fun readU32(): UInt = bytesToUInt(readBytes(4))
|
||||
fun readI32(): Int = bytesToInt(readBytes(4))
|
||||
fun readI16(): Short = bytesToShort(readBytes(2))
|
||||
fun readI64(): Long = bytesToLong(readBytes(8))
|
||||
|
||||
fun readDouble() = Double.fromBits(readI64())
|
||||
|
||||
@@ -20,6 +20,15 @@ fun intToBytes(value: Int): ByteArray {
|
||||
}
|
||||
return result
|
||||
}
|
||||
fun shortToBytes(value: Short): ByteArray {
|
||||
var l = value.toInt()
|
||||
val result = ByteArray(2)
|
||||
for (i in 1 downTo 0) {
|
||||
result[i] = (l and 0xFF).toByte()
|
||||
l = l shr 8
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun uintToBytes(value: UInt): ByteArray {
|
||||
var l = value
|
||||
@@ -52,6 +61,15 @@ fun bytesToInt(b: ByteArray): Int {
|
||||
return result
|
||||
}
|
||||
|
||||
fun bytesToShort(b: ByteArray): Short {
|
||||
var result: Int = 0
|
||||
for (i in 0 until 2) {
|
||||
result = result shl 8
|
||||
result = result or (b[i].toInt() and 0xFF)
|
||||
}
|
||||
return result.toShort()
|
||||
}
|
||||
|
||||
fun bytesToUInt(b: ByteArray): UInt {
|
||||
var result = 0u
|
||||
for (i in 0 until 4) {
|
||||
|
||||
Reference in new issue
Block a user