MidiSheetMusic.MidiFile.VarlenToBytes C# (CSharp) Method

VarlenToBytes() static private method

static private VarlenToBytes ( int num, byte buf, int offset ) : int
num int
buf byte
offset int
return int
        static int VarlenToBytes(int num, byte[] buf, int offset)
        {
            byte b1 = (byte) ((num >> 21) & 0x7F);
            byte b2 = (byte) ((num >> 14) & 0x7F);
            byte b3 = (byte) ((num >>  7) & 0x7F);
            byte b4 = (byte) (num & 0x7F);

            if (b1 > 0) {
            buf[offset]   = (byte)(b1 | 0x80);
            buf[offset+1] = (byte)(b2 | 0x80);
            buf[offset+2] = (byte)(b3 | 0x80);
            buf[offset+3] = b4;
            return 4;
            }
            else if (b2 > 0) {
            buf[offset]   = (byte)(b2 | 0x80);
            buf[offset+1] = (byte)(b3 | 0x80);
            buf[offset+2] = b4;
            return 3;
            }
            else if (b3 > 0) {
            buf[offset]   = (byte)(b3 | 0x80);
            buf[offset+1] = b4;
            return 2;
            }
            else {
            buf[offset] = b4;
            return 1;
            }
        }