Skip to content

Instantly share code, notes, and snippets.

@martindevans
Created August 13, 2012 22:48
Show Gist options
  • Save martindevans/3344632 to your computer and use it in GitHub Desktop.
Save martindevans/3344632 to your computer and use it in GitHub Desktop.
public static void WriteVariableUint64(this BinaryWriter writer, UInt64 value)
{
for (int i = 0; i < sizeof(UInt64); i++)
{
byte writeByte = (byte)(value & 127);
value >>= 7;
if (value != 0)
writeByte |= 128;
writer.Write(writeByte);
if (value == 0)
return;
}
}
public static UInt64 ReadVariableUint64(this BinaryReader reader)
{
UInt64 accumulator = 0;
int iterations = 0;
byte readByte;
do
{
readByte = reader.ReadByte();
accumulator |= ((ulong)readByte & 127) << (iterations * 7);
iterations++;
} while ((readByte & 128) != 0);
return accumulator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment