Update:
I removed the overflow check in the project compiler settings, and it works fine now with the code submitted in the wiki :p
As I said, overflow is normal in this code and everything you should is to disable overflow checking.
I think, you should first read a book about C# and meet every aspect of programing in this language.
I quickly look to: Sams Teach Yourself C# in 21 Days by Bradley Jones on
http://books.google.com and guess what I found (I was looking for: How to ignore overflow errors)?? :PPP
In c# is special clause to force compiler to ignore or to check some expression: checked/unchecked without need to globaly turn overflow checking option on/off :PP
So, everything you need is change both lines from:
lhash += BitConverter.ToInt64(buffer, 0);
to:
unchecked { lhash += BitConverter.ToInt64(buffer, 0); }
So, first read a book about C# syntax ;PP
And Yes, both code works, but that in which you use ulong is better becouse it correctly implement arithmetic operations.
Code: Select all
private static byte[] ComputeMovieHash(Stream input)
{
ulong lhash;
long streamsize;
streamsize = input.Length;
lhash = (ulong)streamsize;
long i = 0;
byte[] buffer = new byte[sizeof(long)];
input.Position = 0;
while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
{
i++;
unchecked { lhash += BitConverter.ToUInt64(buffer, 0); }
}
input.Position = Math.Max(0, streamsize - 65536);
i = 0;
while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
{
i++;
unchecked { lhash += BitConverter.ToUInt64(buffer, 0); }
}
byte[] result = BitConverter.GetBytes(lhash);
Array.Reverse(result);
return result;
}
I removed from code line that is closing stream. If steram is opened outside procedure it should be closed outside too.