Unsigned long long to something longer

This is a discussion on Unsigned long long to something longer within the C Programming forums, part of the General Programming Boards category; Originally Posted by JFonseka Now it gives different hash values for strings longer than 8 characters, but I get an ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by JFonseka View Post
    Now it gives different hash values for strings longer than 8 characters, but I get an odd feeling that it's not right, like that feeling you get when someone's behind with you a knife, but much stronger.
    Unfortunately, with strings >= 8 bytes, you're murdering some part of the world
    Remember that strncpy does not insert a null char if the string is longer than the max! So what you are doing is reading beyond the buffer, looking for data.

    Otherwise, this sample seems unnecessarily complex and does not do what it is intended to do.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define LONGLONGSIZE (sizeof (unsigned long long))
    
    union intstr {
      unsigned long long ikey;
      char               skey[LONGLONGSIZE];
    };
    
    unsigned hashShortString (char v[], unsigned M)
    { 
      union intstr *is;
    
      /* I think you have the idea wrong here. The union is supposed to be 
      used to convert the string into an uint64_t, nothing else. So use a local variable to store 
      the hash.
      Further, you should probably just use your old algorithm on each 8 bytes and append 
      the result to your hash.
      Another thing to think of is what happens if your string doesn't align on 8-byte 
      boundaries. You'll get garbage after the null char, and you probably don't want to has 
      that. */
      for (; *v != '\0'; v++)
           is->ikey = (LONGLONGSIZE*is->ikey + *v) &#37; M;
    
      return is->ikey % M;
    }
    
    int main (int argc, char *argv[])
    {
      char key[LONGLONGSIZE];
    
      if (2 != argc) {
        printf ("USAGE: hash_short STRING\n");
        printf ("  (STRING will be truncated after %u characters)\n",
                LONGLONGSIZE);
        return EXIT_FAILURE;
      }
      strncpy (key, argv[1], LONGLONGSIZE);
      printf ("Hash: %u\n", hashShortString (key, 8191));
    
      return EXIT_SUCCESS;
    }
    Now see my comments in the code.
    And also think about your strcpy in the beginning (why not just pass it directly?).
    Last edited by Elysia; 05-26-2008 at 07:33 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks matsp
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #18
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    The reason being Elysia, that, I didn't write the initial code, my lecturer did. I don't even know what I'm doing this, it just seemed interesting to extend this further because it doesn't copy more than 8, from his. And that append idea seems like a good idea, I will try that.

    Cheers

    And now I get all your murder comments. I guess I am a murderer after all. Lol
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by JFonseka View Post
    The reason being Elysia, that, I didn't write the initial code, my lecturer did. I don't even know what I'm doing this, it just seemed interesting to extend this further because it doesn't copy more than 8, from his. And that append idea seems like a good idea, I will try that.
    Yep, but the original code couldn't hash the entire string, so either way, you have to be inventing some new code which may require a change in your code structure!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21