示例#1
0
 // it could only distinguish unsigned varint32 and varint8
 public static byte[] ToVarint(uint value, VarintType type)
 {
     if (type == VarintType.Varint8)
     {
         return(VarintBitConverter.GetVarintBytes((byte)value));
     }
     else
     {
         return(VarintBitConverter.GetVarintBytes(value));
     }
 }
示例#2
0
 // it could only distinguish unsigned varint32 and varint8
 public static (byte[], VarintType) ToVarint(uint value)
 {
     if (value < 0x80)
     {
         return(VarintBitConverter.GetVarintBytes((byte)value), VarintType.Varint8);
     }
     else
     {
         return(VarintBitConverter.GetVarintBytes(value), VarintType.Varint32);
     }
 }
示例#3
0
 public static int GetVarintSize(uint number)
 {
     (byte[] _, VarintType type) = VarintBitConverter.ToVarint(number);
     if (type == VarintType.Varint8)
     {
         return(1);
     }
     else if (type == VarintType.Varint32)
     {
         return(4);
     }
     else
     {
         throw new Exception($"Varint type {type} does not exist");
     }
 }
示例#4
0
        // it could only distinguish unsigned varint32 and varint8
        public static (uint, VarintType) FromVarint(byte[] data, int startIndex)
        {
            VarintType type;
            uint       value;

            // most significant bit is 1
            if ((data[startIndex] & 0x80) != 0)
            {
                type = VarintType.Varint32;

                value = VarintBitConverter.ToUInt32(data, startIndex);
            }
            // most significant bit is 0
            else
            {
                type  = VarintType.Varint8;
                value = data[startIndex];
            }

            return(value, type);
        }