Beispiel #1
0
        /// <summary>
        /// Encodes a number in the Base64 VLQ format
        /// </summary>
        /// <param name="value">The value to be encoded</param>
        /// <returns>The value encoded as a Base64 VLQ string</returns>
        public static string Encode(int value)
        {
            var encoded = new List <char>();
            var vlq     = toVLQ(value);

            do
            {
                var digit = vlq & VLQ_BASE_MASK;
                vlq = vlq >> VLQ_BASE_SHIFT;
                if (vlq > 0)
                {
                    digit |= VLQ_CONTINUATION_BIT;
                }

                encoded.Add(Base64Converter.Encode(digit));
            }while (vlq > 0);
            return(string.Concat(encoded));
        }
Beispiel #2
0
        public static DecodeResult Decode(string encoded)
        {
            int result = 0;
            int shift  = 0;

            int charactersRead = 0;

            foreach (var character in encoded)
            {
                charactersRead += 1;

                var digit        = Base64Converter.Decode(character);
                var continuation = (digit & VLQ_CONTINUATION_BIT) != 0;
                digit  &= VLQ_BASE_MASK;
                result += (digit << shift);
                shift  += VLQ_BASE_SHIFT;
                if (!continuation)
                {
                    break;
                }
            }
            return(new DecodeResult(fromVLQ(result), charactersRead));
        }