Ejemplo n.º 1
0
        public bitField littleEndianValue()
        {
            int      shift  = 0;
            int      len    = bytes.Length;
            bitField retVal = new bitField(nbits, bytes);

            if (nbits <= 8)
            {
                return(retVal);
            }

            if (nbits % 8 != 0)
            {
                shift = (8 - nbits % 8);
            }
            retVal <<= shift;
            for (int i = 0; i < len / 2; i++)
            {
                byte tmp = retVal.bytes[i];

                retVal.bytes[i]           = retVal.bytes[len - i - 1];
                retVal.bytes[len - i - 1] = tmp;
            }
            retVal.bytes[0] >>= shift;

            return(retVal);
        }
Ejemplo n.º 2
0
        private object getNext(Field f)
        {
            int bytesToRead = (int)Math.Ceiling((f.bitwidth - 8 + lastBits) / 8.0);

            byte[] newBytes;
            uint   locLastBits = lastBits;

            newBytes    = new byte[bytesToRead + 1];
            newBytes[0] = lastByte;

            fStream.Read(newBytes, 1, bytesToRead);             // TODO: do something if bytes_read < bytesToRead
            if (bytesToRead > 0)
            {
                lastByte = newBytes[newBytes.Length - 1];
            }

            lastBits = (uint)f.bitwidth + lastBits - 8 * (uint)bytesToRead;

            bitField retVal     = new bitField(f.bitwidth);
            int      bitsUsed   = 0;
            int      bitCounter = 0;

            while (bitsUsed < f.bitwidth)
            {
                byte curByte = newBytes[bitCounter / 8];
                uint curBit;

                if (bitCounter < locLastBits)
                {
                    bitCounter++;
                    continue;
                }

                curByte <<= (bitCounter % 8);
                curBit    = (curByte & (0x80u)) >> 7;

                retVal <<= 1;
                retVal  |= curBit;
                bitsUsed++;
                bitCounter++;
            }
            if (!f.endianness)
            {
                retVal = retVal.littleEndianValue();
            }

            switch (f.type)
            {
            case "uint":
                if (f.bitwidth <= 64)
                {
                    return((ulong)retVal);
                }
                else
                {
                    return(retVal);
                }

            case "sint":
                if (f.bitwidth <= 64)
                {
                    return((long)retVal);
                }
                else
                {
                    return(retVal);
                }
            }
            return(0);
        }