By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,673 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,673 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" <ma********************@mvps.org> wrote in message
news:eg**************@tk2msftngp13.phx.gbl...
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 13 '05 #2

P: n/a
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" <ds*******@cox.net> wrote in message
news:sVmQa.1004$Ze.581@fed1read03...
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" <ma********************@mvps.org> wrote in message
news:eg**************@tk2msftngp13.phx.gbl...
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


Nov 13 '05 #3

P: n/a

"Dmitri Shvetsov" <ds*******@cox.net> wrote in message
news:sVmQa.1004$Ze.581@fed1read03...
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.
bytes and another one is a text file with 600 kbytes length.

Nov 13 '05 #4

P: n/a
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.
Nov 13 '05 #5

P: n/a
Dmitri Shvetsov <ds*******@cox.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #6

P: n/a
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Dmitri Shvetsov <ds*******@cox.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #7

P: n/a
Dmitri Shvetsov <ds*******@cox.net> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #8

P: n/a
Thanks a lot, it's the best answer, the question is closed.

Regards,
Dmitri.

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com>
Array.GetHashCode returns the hash code of the Byte Array object itself. Not the hash of the values in the byte array.

String overrides GetHashCode to return a value based on the individual char values in the string. Not the string object itself.

Try this simple program:

Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.