Exemplo n.º 1
0
        public string ReadString(int bitlength, TextEncoding enc = TextEncoding.Unicode)
        {
            if (this.Length < (Position + bitlength))
            {
                throw new InvalidOperationException("Requested bit length is out of range.");
            }
            string   result = "";
            BitArray myBits = new BitArray(bitlength);

            for (int i = 0; i < bitlength; i++)
            {
                myBits.Set(i, this[Position + i]);
            }
            byte[] buffer = BitArrayToByteArray(myBits);
            if (enc.HasFlag(TextEncoding.Unicode) && enc.HasFlag(TextEncoding.BigEndian))
            {
                result = System.Text.Encoding.BigEndianUnicode.GetString(buffer);
            }
            else if (enc.HasFlag(TextEncoding.Unicode))
            {
                result = System.Text.Encoding.Unicode.GetString(buffer);
            }
            else if (enc.HasFlag(TextEncoding.UTF32))
            {
                result = System.Text.Encoding.UTF32.GetString(buffer);
            }
            else if (enc.HasFlag(TextEncoding.ASCII))
            {
                result = System.Text.Encoding.ASCII.GetString(buffer);
            }
            else if (enc.HasFlag(TextEncoding.UTF7))
            {
                result = System.Text.Encoding.UTF7.GetString(buffer);
            }
            else if (enc.HasFlag(TextEncoding.UTF8))
            {
                result = System.Text.Encoding.UTF8.GetString(buffer);
            }
            Position += bitlength;
            return(result);
        }