//byte数组转成Int值 由高位到低位
fun byteArrayToInt(params:ByteArray): Int {
var value = 0
for (i in 0..3) {
val shift = (4 - 1 - i) * 8
value += params[i].toInt() and 0x000000FF shl shift
}
return value
}
//byte数组转成Int值 由低位到高位
fun byteArrayToIntLH(data:ByteArray): Int{
var count: Int = data[0].toInt() and 0xff
count = count or (data[1].toInt() and 0xff shl 8)
count = count or (data[2].toInt() and 0xff shl 16)
count = count or (data[3].toInt() and 0xff shl 24)
return count
}
//byte数组转short,高位在前,低位在后
fun byteArrayToShort(param:ByteArray): Short {
var value = (param[0].toInt() and 0xFF) shl 8
value = value or (param[1].toInt() and 0xff)
return value.toShort()
}
//byte数组转short,低位在前,高位在后
fun byteToShortLittle(b: ByteArray): Short {
return ((b[1].toInt() shl 8) or (b[0].toInt() and 0xff)).toShort()
}
//byte数组转成Long值 由低位到高位
fun byteArrayToLongLH(param:ByteArray): Long{
var result = 0L
for (i in 0..7){
result += ((param[i].toLong() and 0xff) shl ((8-1-(7-i))*8))
}
return result
}
//byteArray转Long,高位在前,低位在后
fun byteArrayToLong(param:ByteArray): Long{
var result = 0L
for (i in 0..7){
result += ((param[i].toLong() and 0xff) shl ((8-1-i)*8))
}
return result
}
|