Skip to content

Instantly share code, notes, and snippets.

@Scellow
Created November 4, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scellow/4724042f18e9e441138d839cd089dc63 to your computer and use it in GitHub Desktop.
Save Scellow/4724042f18e9e441138d839cd089dc63 to your computer and use it in GitHub Desktop.
import kotlinx.cinterop.bitsToFloat
class BitConverter
{
companion object
{
// region toBytes
fun longToBytes(value: Long, target: ByteArray, position: Int): ByteArray
{
target[position + 0] = (value ushr 56).toByte()
target[position + 1] = (value ushr 48).toByte()
target[position + 2] = (value ushr 40).toByte()
target[position + 3] = (value ushr 32).toByte()
target[position + 4] = (value ushr 24).toByte()
target[position + 5] = (value ushr 16).toByte()
target[position + 6] = (value ushr 8).toByte()
target[position + 7] = (value).toByte()
return target
}
fun intToBytes(value: Int, target: ByteArray, position: Int): ByteArray
{
target[position + 0] = (value shr 24).toByte()
target[position + 1] = (value shr 16).toByte()
target[position + 2] = (value shr 8).toByte()
target[position + 3] = (value).toByte()
return target
}
fun floatToBytes(value: Float, target: ByteArray, position: Int): ByteArray
{
val asInt = value.bits()
target[position + 0] = (asInt shr 24).toByte()
target[position + 1] = (asInt shr 16).toByte()
target[position + 2] = (asInt shr 8).toByte()
target[position + 3] = (asInt).toByte()
return target
}
fun shortToBytes(value: Short, target: ByteArray, position: Int): ByteArray
{
target[position + 0] = (value.toInt() shr 8).toByte()
target[position + 1] = (value).toByte()
return target
}
// endregion
// region toType
fun toLong(source:ByteArray, position: Int):Long
{
return ((source[position + 0].toLong() and 0xffL shl 56) or ((source[position + 1].toLong() and 0xffL) shl 48) or
((source[position + 2].toLong() and 0xffL) shl 40) or ((source[position + 3].toLong() and 0xffL) shl 32) or
((source[position + 4].toLong() and 0xff) shl 24) or ((source[position + 5].toLong() and 0xff) shl 16) or
((source[position + 6].toLong() and 0xff) shl 8) or ((source[position + 7].toLong() and 0xff)))
}
fun toInt(source:ByteArray, position: Int):Int
{
return ((source[position + 0].toInt() and 0xff) shl 24) or
((source[position + 1].toInt() and 0xff) shl 16) or
((source[position + 2].toInt() and 0xff) shl 8) or
((source[position + 3].toInt() and 0xff))
}
fun toFloat(source:ByteArray, position: Int):Float
{
val asInt = toInt(source, position)
return bitsToFloat(asInt)
}
fun toShort(source:ByteArray, position: Int):Short
{
return (((source[position + 0].toInt() and 0xff) shl 8) or
((source[position + 1].toInt() and 0xff))).toShort()
}
// endregion
}
}
class Reader(private val source: ByteArray, startIndex: Int = 0)
{
private var position: Int = startIndex
val length: Int = source.size
fun readByte(): Byte
{
val ret = source[position]
position += 1
return ret
}
fun readShort(): Short
{
val ret = BitConverter.toShort(source, position)
position += 2
return ret
}
fun readInt(): Int
{
val ret = BitConverter.toInt(source, position)
position += 4
return ret
}
fun readFloat(): Float
{
val ret = BitConverter.toFloat(source, position)
position += 4
return ret
}
fun readLong(): Long
{
val ret = BitConverter.toLong(source, position)
position += 8
return ret
}
}
class Writter(private val source: ByteArray, startIndex: Int = 0)
{
private var position: Int = startIndex
val length: Int = source.size
fun writeByte(value: Byte)
{
source[position] = value
position += 1
}
fun writeShort(value: Short)
{
BitConverter.shortToBytes(value, source, position)
position += 2
}
fun writeInt(value: Int)
{
BitConverter.intToBytes(value, source, position)
position += 4
}
fun writeFloat(value: Float)
{
BitConverter.floatToBytes(value, source, position)
position += 4
}
fun writeLong(value: Long)
{
BitConverter.longToBytes(value, source, position)
position += 8
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment