tags:

views:

31

answers:

1

Has anyone worked with .Net code to read/manage VLQs? There is a Java method readMultiByteInteger that accepts a stream of bytes and coverts them to ints according to VLQ spec but i cannot find a C# equivalent.

From Wikipedia: A variable-length quantity (VLQ) is a universal code that uses an arbitrary number of binary octets (eight-bit bytes) to represent an infinitely large integer.

Anyone have an idea? (Besides switching to Java?)

Thanks, Paul

+1  A: 

Hi I have created the Extension method to convert int -> uintvar, theres no inbuilt support to do that...

public static class BinaryExtension
{
  public static IEnumerable<int> ToUIntVar(this int value)
  {
     string binary = Convert.ToString(value, 2);    
     for (int i = binary.Length; i > 0; i -= 7)
     {
        if (i >= 7)
        {
           if (i == binary.Length)
              yield return Convert.ToInt32(binary.Substring(i - 7, 7).PadLeft(8, '0'), 2);
           else
               yield return Convert.ToInt32("1" + binary.Substring(i - 7, 7), 2);
         }
         else if (binary.Length < 7)
           yield return Convert.ToInt32(binary.Substring(0, i).PadLeft(8, '0'), 2);
         else
           yield return Convert.ToInt32("1" + binary.Substring(0, i).PadLeft(7, '0'), 2);
      }
  }
}

I know code looks ugly, but it works with C# 3.0, if you want use this code with C# 2.0 I assume that you know the procedure...

How to use

var uIntVars = 0x0FFFFFFF.ToUIntVar();
Prashant
Thanks Prashant. Unfortunatelly i'm still working with .Net 2.0 but i can retrofit your code. e.g. i can make it a static utility method rather than an extension. Thanks again.
Paul Sasik
Yeah you can make it static method and also can clean some ugly code
Prashant