All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.binaryfoo.decoders.bit.NumericBitStringField.kt Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
package io.github.binaryfoo.decoders.bit

import io.github.binaryfoo.bit.EmvBit

import java.util.BitSet

/**
 * An integer masked out of a number of bits in a single byte.
 */
public class NumericBitStringField(private val byteNumber: Int, private val firstBit: Int, private val lastBit: Int, private val name: String) : BitStringField {

    {
        if (lastBit > firstBit) {
            throw IllegalArgumentException("Must be left to right order: " + lastBit + " > " + firstBit)
        }
    }

    override public fun getPositionIn(bits: Set?): String {
        return "Byte ${byteNumber} Bits ${firstBit}-${lastBit}"
    }

    override public fun getValueIn(bits: Set): String {
        val theByte = BitSet(8)
        for (bit in bits) {
            if (bit.byteNumber == byteNumber && bit.bitNumber <= firstBit && bit.bitNumber >= lastBit) {
                theByte.set(bit.bitNumber - 1, bit.set)
            }
        }
        val bytes = theByte.toByteArray()
        val i = if (bytes.size == 0) 0 else (bytes[0].toInt() and 255).ushr((lastBit - 1))
        return "${name} = ${i}"
    }

    override fun getStartBytesOffset(): Int = byteNumber - 1

    override fun getLengthInBytes(): Int  = 1
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy