byte[] ba.GetHashCode() -- Curious result!-)

D

Dmitri Shvetsov

Hi All,

Did somebody try to get the Hash code from byte array? The method exists,
the program is compilable but...)))

Try!-) Every time you can get the different code from the same array, and
always very short, like in range from 10 to 18 in my case. Maybe this method
has no realization?

P.S. To get a correct result I use transformation of byte[] to string and
then string s.GetHashCode();

Any comments?

Regards,
Dmitri.
 
D

Dmitri Shvetsov

No exactly, it's a part of method. I read a long file as a byte array, then
I try to get Hash from this array and every time I get this problem that I
described earlier. I used 2 files to be sure, one is about 8 bytes of real
bytes and another one is a text file with 600 kbytes length.

So, every time I allocate a different memory and load this file into this
memory as byte[].

Dmitri.

Mattias Sjögren said:
Dmitri,
Did somebody try to get the Hash code from byte array? The method exists,
the program is compilable but...)))

Try!-)

I just did...

Every time you can get the different code from the same array,

and I don't see the behavior your describing with v1.1 of the
framework. This prints the same code ten times.

byte[] arr = new byte[] {0,1,2,3,4,5};
for ( int i = 0; i < 10; i++ )
Console.WriteLine( arr.GetHashCode() );

You're not allocating a new array every time you check, are you?



Mattias
 
D

Dmitri Shvetsov

Forgot to add. After reading I write this file into new file to check the
contents of 2 files. They are equal. That's strange.

When I get a string from this byte[] the Hash in both cases is equal and
very long number instead of 1-byte as before.

Dmitri.

Dmitri Shvetsov said:
No exactly, it's a part of method. I read a long file as a byte array, then
I try to get Hash from this array and every time I get this problem that I
described earlier. I used 2 files to be sure, one is about 8 bytes of real
bytes and another one is a text file with 600 kbytes length.

So, every time I allocate a different memory and load this file into this
memory as byte[].

Dmitri.

Mattias Sjögren said:
Dmitri,
Did somebody try to get the Hash code from byte array? The method exists,
the program is compilable but...)))

Try!-)

I just did...

Every time you can get the different code from the same array,

and I don't see the behavior your describing with v1.1 of the
framework. This prints the same code ten times.

byte[] arr = new byte[] {0,1,2,3,4,5};
for ( int i = 0; i < 10; i++ )
Console.WriteLine( arr.GetHashCode() );

You're not allocating a new array every time you check, are you?



Mattias
 
D

Dmitri Shvetsov

Dmitri Shvetsov said:
No exactly, it's a part of method. I read a long file as a byte array, then
I try to get Hash from this array and every time I get this problem that I
described earlier. I used 2 files to be sure, one is about 8 bytes of real

8 Kbytes, sorry.
 
R

Rob Tillie

P.S. To get a correct result I use transformation of byte[] to string
and then string s.GetHashCode();

Why not use byte[].GetHashCode? Why are you transforming it into a string?

Greetz,
-- Rob.
 
J

Jon Skeet

Dmitri Shvetsov said:
No exactly, it's a part of method. I read a long file as a byte array, then
I try to get Hash from this array and every time I get this problem that I
described earlier. I used 2 files to be sure, one is about 8 bytes of real
bytes and another one is a text file with 600 kbytes length.

So, every time I allocate a different memory and load this file into this
memory as byte[].

Ah. That's not what you originally described. You described taking a
hash code from the *same array* - now you're talking about taking a
hash code from a *different* array with the same values.

Basically, Array doesn't override GetHashCode - it just inherits the
implementation from object. It doesn't override Equals, either -
basically, to compare two arrays, you need to compare their elements.
 
D

Dmitri Shvetsov

When I load this byte array from the same file every time I expect to get
the same result in the GetHashCode() from this byte array. I don't care that
this array was loaded into another memory address, physically these bytes
are the same. I get different Hash Code values every time when I reload this
file of bytes and it seems that these values are wrong at all, because
values are small like 14, 18 and repeat in some loop. When I transform in
very next step this byte[] to string and get Hash from this string it's
always the same from this byte[], although the Hash from this byte[] is
different every time.

That's a case that I can't explain. And that's why I use transformation of
byte[] to string to get the same result every time from the same string.

Maybe it's only in my PC? Service Pack 4, Win2000Pro, VS2003...

Dmitri.

Jon Skeet said:
Dmitri Shvetsov said:
No exactly, it's a part of method. I read a long file as a byte array, then
I try to get Hash from this array and every time I get this problem that I
described earlier. I used 2 files to be sure, one is about 8 bytes of real
bytes and another one is a text file with 600 kbytes length.

So, every time I allocate a different memory and load this file into this
memory as byte[].

Ah. That's not what you originally described. You described taking a
hash code from the *same array* - now you're talking about taking a
hash code from a *different* array with the same values.

Basically, Array doesn't override GetHashCode - it just inherits the
implementation from object. It doesn't override Equals, either -
basically, to compare two arrays, you need to compare their elements.
 
J

Jon Skeet

Dmitri Shvetsov said:
When I load this byte array from the same file every time I expect to get
the same result in the GetHashCode() from this byte array.

There is no "this byte array". Again, you're making it *sound* like
you've got a single byte array, when in fact you mean something like
"when I load this sequence of bytes into a byte array I expect to get
the same result in the GetHashCode() from each byte array loaded with
this sequence of bytes."
I don't care that
this array was loaded into another memory address, physically these bytes
are the same.

You may not, but the system does. It is *not* the same byte array, even
if the contents are identical.
I get different Hash Code values every time when I reload this
file of bytes and it seems that these values are wrong at all, because
values are small like 14, 18 and repeat in some loop. When I transform in
very next step this byte[] to string and get Hash from this string it's
always the same from this byte[], although the Hash from this byte[] is
different every time.

That's because System.String *does* override GetHashCode - two separate
string instances which contain the same data will give the same hash
code. That isn't true of byte arrays.

Note that hash codes are most useful when it comes to immutable objects
- things like Hashtable may well cache the hash value, assuming it will
stay the same (for efficiency) but if the hash code changes, that would
give very strange results.
That's a case that I can't explain. And that's why I use transformation of
byte[] to string to get the same result every time from the same string.

Why not just write your own code to generate a hashcode, or maybe apply
something like an MD5 hash to the data?
 
D

Dmitri Shvetsov

Thanks a lot, it's the best answer, the question is closed.

Regards,
Dmitri.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top