473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
8 8195
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
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

"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.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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William Kossack | last post by:
How can I write a query to return non distinct results?
47
by: Kapil Khosla | last post by:
Hi, I am trying to reverse a byte eg. 11010000 should look like 00001011 Plz note, it is not a homework problem and I do not need the c code for it. Just give me an idea how should I proceed...
4
by: Guinness Mann | last post by:
resource.cs(8,15): warning CS0659: 'UA.LMS.Resource.Resource' overrides Object.Equals(object o) but does not override Object.GetHashCode() What does this mean? I mean I know what it says, but do...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
3
by: news.microsoft.com | last post by:
Hello, I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue. Steping through both the C# and VB.NET apps i get identical results but for one section. Notes: bit_buffer = 360...
1
by: William | last post by:
I have a strongly typed XML document created with xsd.exe. I have a web request that returns an xml document as a byte. I would like to desearlize the return byte array but not quite sure how. I am...
4
by: Andrew Robinson | last post by:
I have a class that has three properties: two of type int and one of type string. Is this the best method when overriding the GetHashCode() ? I am guessing not... any thing better? public...
4
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have an RGB color held in a Int32 variable. I using an unsafe code to loop through Bitmap data, and by using the BitmapData.Scan0 pointer I need to copy the Int32 color value into the Bitmap...
2
by: Polaris | last post by:
Hi C# Experts: I have a byte-array: byte byte_array = new byte; byte_array = 0; byte_array = 1; byte_array = 2; byte_array = 3;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.