Decode Stream to ulong - CSharp System.IO

CSharp examples for System.IO:Stream

Description

Decode Stream to ulong

Demo Code

// The MIT License (MIT)
using System.IO;/*from www.j  a  v  a2s  .c  om*/
using System;

public class Main{
        private static ulong Decode(Stream stream, int maxBytes)
            {
                var currentShift = 0;
                var bytesRead = 0;
                ulong finalValue = 0;

                while (bytesRead <= maxBytes)
                {
                    var nextByte = stream.ReadByte();
                    bytesRead++;

                    finalValue |= ((ulong)(nextByte & 0x7F) << currentShift);
                    currentShift += 7;

                    if ((nextByte & 0x80) == 0)
                    {
                        break;
                    }
                }

                return finalValue;
            }
}

Related Tutorials