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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dmitri Shvetsov

    #1

    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.


  • Dmitri Shvetsov

    #2
    Re: byte[] ba.GetHashCode( ) -- Curious result!-)

    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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
    news:egcqn0ZSDH A.2316@tk2msftn gp13.phx.gbl...[color=blue]
    > Dmitri,
    >[color=green]
    > >Did somebody try to get the Hash code from byte array? The method exists,
    > >the program is compilable but...)))
    > >
    > >Try!-)[/color]
    >
    > I just did...
    >
    >[color=green]
    > >Every time you can get the different code from the same array,[/color]
    >
    > 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.WriteLi ne( 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.[/color]


    Comment

    • Dmitri Shvetsov

      #3
      Re: byte[] ba.GetHashCode( ) -- Curious result!-)

      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" <dshvetsov@cox. net> wrote in message
      news:sVmQa.1004 $Ze.581@fed1rea d03...[color=blue]
      > No exactly, it's a part of method. I read a long file as a byte array,[/color]
      then[color=blue]
      > 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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      > news:egcqn0ZSDH A.2316@tk2msftn gp13.phx.gbl...[color=green]
      > > Dmitri,
      > >[color=darkred]
      > > >Did somebody try to get the Hash code from byte array? The method[/color][/color][/color]
      exists,[color=blue][color=green][color=darkred]
      > > >the program is compilable but...)))
      > > >
      > > >Try!-)[/color]
      > >
      > > I just did...
      > >
      > >[color=darkred]
      > > >Every time you can get the different code from the same array,[/color]
      > >
      > > 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.WriteLi ne( 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.[/color]
      >
      >[/color]


      Comment

      • Dmitri Shvetsov

        #4
        Re: byte[] ba.GetHashCode( ) -- Curious result!-)


        "Dmitri Shvetsov" <dshvetsov@cox. net> wrote in message
        news:sVmQa.1004 $Ze.581@fed1rea d03...[color=blue]
        > No exactly, it's a part of method. I read a long file as a byte array,[/color]
        then[color=blue]
        > 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[/color]

        8 Kbytes, sorry.
        [color=blue]
        > bytes and another one is a text file with 600 kbytes length.[/color]


        Comment

        • Rob Tillie

          #5
          Re: byte[] ba.GetHashCode( ) -- Curious result!-)

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

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

          Greetz,
          -- Rob.


          Comment

          • Jon Skeet

            #6
            Re: byte[] ba.GetHashCode( ) -- Curious result!-)

            Dmitri Shvetsov <dshvetsov@cox. net> wrote:[color=blue]
            > 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[].[/color]

            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 - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Dmitri Shvetsov

              #7
              Re: byte[] ba.GetHashCode( ) -- Curious result!-)

              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" <skeet@pobox.co m> wrote in message
              news:MPG.197c6f 204418f5f298a0c a@news.microsof t.com...[color=blue]
              > Dmitri Shvetsov <dshvetsov@cox. net> wrote:[color=green]
              > > No exactly, it's a part of method. I read a long file as a byte array,[/color][/color]
              then[color=blue][color=green]
              > > I try to get Hash from this array and every time I get this problem that[/color][/color]
              I[color=blue][color=green]
              > > described earlier. I used 2 files to be sure, one is about 8 bytes of[/color][/color]
              real[color=blue][color=green]
              > > 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[/color][/color]
              this[color=blue][color=green]
              > > memory as byte[].[/color]
              >
              > 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 - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet/
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet

                #8
                Re: byte[] ba.GetHashCode( ) -- Curious result!-)

                Dmitri Shvetsov <dshvetsov@cox. net> wrote:[color=blue]
                > 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.[/color]

                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."
                [color=blue]
                > I don't care that
                > this array was loaded into another memory address, physically these bytes
                > are the same.[/color]

                You may not, but the system does. It is *not* the same byte array, even
                if the contents are identical.
                [color=blue]
                > 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.[/color]

                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.
                [color=blue]
                > 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.[/color]

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

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Dmitri Shvetsov

                  #9
                  Re: byte[] ba.GetHashCode( ) -- Curious result!-)

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

                  Regards,
                  Dmitri.

                  "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com>
                  [color=blue]
                  > Array.GetHashCo de returns the hash code of the Byte Array object itself.[/color]
                  Not[color=blue]
                  > the hash of the values in the byte array.
                  >
                  > String overrides GetHashCode to return a value based on the individual[/color]
                  char[color=blue]
                  > values in the string. Not the string object itself.
                  >
                  > Try this simple program:[/color]


                  Comment

                  Working...