ToString -> upper and lower

  • Thread starter Thread starter Pohihihi
  • Start date Start date
P

Pohihihi

I am getting Guid using

new Guid()

when I say

new Guid().ToString() I get 10856f25-2759-0358-159c-ff7e0759c800

but when I say

new Guid().ToString().ToUpper() I get following

D98C1DD4-008F-04B2-E980-0998ECF8427E

what is going on?
 
Firstly, if you create a guid with empty constructor "new Guid()" you should
get only zeros...
I believe you are using System.Guid.NewGuid(), and everytime you run this
you are supposed to get a unique guid... so they will always be differrent.

HTH,

Erick Sgarbi
www.blog.csharpbox.com
 
I should have paste the code. Anyways, all this time I was trying to find
the reason and it is as following after that code

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\h.html");
Stream fs = fi.Open(FileMode.Open,FileAccess.Read,FileShare.None);
MD5 hash = new MD5CryptoServiceProvider();
byte[] b = hash.ComputeHash(fs);
byte[] a = hash.ComputeHash(fs);

This code calls ComputeHash on fs 2 times. Both time I get different byte
array.

Is it cause ComputeHash is not reading the file till the end and calculates
the hash code on what ever is read the first time and then second time it
reads rest of the buffer and recalculates?

What I am doing wrong?
 
I have a test code that shows ComputeHash read to the end of the
stream: in the following code you can see b is the same with c, you can
try d,e,f,g,.... and it will be the same value of b & c.

static void WriteBytes(byte[] bytes)
{
foreach (byte b in bytes)
{
Console.Write(b.ToString() + " ");
}
Console.WriteLine();
}

static void Main(String[] args)
{
System.IO.FileInfo fi = new System.IO.FileInfo(@"d:\test\test.txt");
Stream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None);
MD5 hash = new MD5CryptoServiceProvider();
byte[] a = hash.ComputeHash(fs);
Console.WriteLine(fs.Position.ToString() + " ?= " +
fs.Length.ToString());
byte[] b = hash.ComputeHash(fs);
byte[] c = hash.ComputeHash(fs);

WriteBytes(a);
WriteBytes(b);
WriteBytes(c);
Console.ReadLine();
}
 
RealFun,

Yes I know position is eq to length but a and b are not same. Again, in my
original problem, I am not getting the same value for a and b.

b, c, d,... are same and that is ^not^ what I am asking.

thanks,
Po
 
Pohihihi said:
I should have paste the code. Anyways, all this time I was trying to find
the reason and it is as following after that code

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\h.html");
Stream fs = fi.Open(FileMode.Open,FileAccess.Read,FileShare.None);
MD5 hash = new MD5CryptoServiceProvider();
byte[] b = hash.ComputeHash(fs);
byte[] a = hash.ComputeHash(fs);

This code calls ComputeHash on fs 2 times. Both time I get different byte
array.

Yes, I'm not surprised - you're reading the whole file in first, so b
gets a proper hash, but then you're reading using the same stream,
which has already finished - so a is the hash of nothing, basically.
 
Thanks Jon, you gave me the reason I was looking for.


Jon Skeet said:
Pohihihi said:
I should have paste the code. Anyways, all this time I was trying to find
the reason and it is as following after that code

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\h.html");
Stream fs = fi.Open(FileMode.Open,FileAccess.Read,FileShare.None);
MD5 hash = new MD5CryptoServiceProvider();
byte[] b = hash.ComputeHash(fs);
byte[] a = hash.ComputeHash(fs);

This code calls ComputeHash on fs 2 times. Both time I get different byte
array.

Yes, I'm not surprised - you're reading the whole file in first, so b
gets a proper hash, but then you're reading using the same stream,
which has already finished - so a is the hash of nothing, basically.
 
Jon Skeet [ C# MVP ],
woo...he said it's not his question

Where, exactly? It looked like his question really was about why a and
b are not the same, and that was what I answered...
 
hehe
I have just said "I have a test code that shows ComputeHash read to the
end of the stream"
and he replied that is not about the quetion, so i think perhaps he is
asking about other things

:)
 
hehe
I have just said "I have a test code that shows ComputeHash read to the
end of the stream"
and he replied that is not about the quetion, so i think perhaps he is
asking about other things

Evidently not, given his other reply. I think the problem was that
while you understood what the problem was, your reply didn't make it
obvious to him.
 
Back
Top