Created
August 13, 2012 22:48
-
-
Save martindevans/3344632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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