Exemple #1
0
        public static char[] Encode(int bitsPerChar, EncodeCallback encodeCallback,
		                            byte[] value, int bitsToEncode)
        {
            Helper.CheckRange("bitsPerChar", bitsPerChar, 1, 8);
            Helper.CheckNull("encodeCallback", encodeCallback);
            Helper.CheckNull("value", value);
            Helper.CheckRange("bitsToEncode", bitsToEncode, 0, int.MaxValue - (bitsPerChar - 1));
            char[] chars = new char[(bitsToEncode + bitsPerChar - 1)/bitsPerChar];

            int i = 0, j = 0, bits = 0, buffer = 0;
            while (bitsToEncode > 0)
            {
                if (bits < 6 && i < value.Length)
                {
                    bits += 8;
                    buffer <<= 8;
                    buffer |= value[i++];
                }

                int chunk = Math.Min(bitsPerChar, Math.Min(bits, bitsToEncode));
                bits -= chunk;
                bitsToEncode -= bitsPerChar;

                int pow2Bits = buffer >> bits;
                buffer ^= pow2Bits << bits;
                chars[j++] = encodeCallback(pow2Bits << (bitsPerChar - chunk));
            }

            Debug.Assert(j == chars.Length);
            return chars;
        }
Exemple #2
0
        public static char[] Encode(int bitsPerChar, EncodeCallback encodeCallback,
		                            byte[] value)
        {
            Helper.CheckNull("value", value);
            Helper.CheckRange("value", value, 0, int.MaxValue/8);
            return Encode(bitsPerChar, encodeCallback, value, value.Length*8);
        }