Thread: What hashing algorithm outputs hash value as numbers only?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    What hashing algorithm outputs hash value as numbers only?

    Hi, Gurus. What hashing algorithm outputs hash value as numbers only? For example, if you pass a “John Q. Public” it will output 23324. If there is no such hashing, how hard is it to hire somebody to write a fairly quick one? It could be some fast hashing and then another function that creates numbers. Much obliged. wkatz.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What hashing algorithm outputs hash value as numbers only?
    If you remove the restriction on "only", I think it would be all of them. Otherwise, it would be none of them, insofar as non-numeric data can be mapped to numbers and vice versa.

    If there is no such hashing, how hard is it to hire somebody to write a fairly quick one?
    Impossible, obviously. If it cannot be done, not even if you offer your soul as the price could it be done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    Thanks, Laserlight!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > What hashing algorithm outputs hash value as numbers only?
    All of them I would say.

    > if you pass a “John Q. Public” it will output 23324.
    A very basic one would be just add up the ASCII values of all the letters.
    Like 'J' + 'o' + 'h' + 'n' etc etc

    Code:
    int hash ( const char *string ) {
      int result = 0;
      while ( *string ) result += *string++;
      return result;
    }
    Now, where's my money?

    A good hash is another matter, one which doesn't produce obvious collisions for all anagrams for example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What hashing algorithm outputs hash value as numbers only?
    That's the definition of a hash function. It maps keys to integers that can be used as the index for a table. You probably want an algorithm that's tuned for strings, like shift-add-xor:
    Code:
    unsigned sax_hash ( void *key, int len )
    {
      unsigned char *p = key;
      unsigned h = 0;
      int i;
    
      for ( i = 0; i < len; i++ )
        h ^= ( h << 5 ) + ( h >> 2 ) + p[i];
    
      return h;
    }
    But any good algorithm will should work well. You seem to be mildly confused about hashing and hash tables, so browse this and this for some basic info.
    My best code is written with the delete key.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Try pasing the characters of the string through a CRC algorithm.
    The CRC of the string gives a pretty good distribution.
    Just pick an appropriate sized CRC for the number of items to hash.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Could do a basic hash that outputs to alphanumerics then convert each char to a number and use that.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    EDIT: Mentioned by Wraithan.

    What kind of hash do you want? A checksum? Hash for a table? If you want a cryptographic hash, well, you're better off with a string. A teensy weensy integer is clearly not enough.
    Last edited by jafet; 09-11-2006 at 04:30 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All cryptographic hashes in use result in integers. They just happen to be very BIG integers. (SHA-256, as the name says, results in 256-bit-wide integers. That's a LOT.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with unaligned intrinsics
    By The Wazaa in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2009, 12:36 PM
  2. int vector outputs wrong numbers
    By Vandrian in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 12:15 PM
  3. hashing help
    By alokin in forum C Programming
    Replies: 17
    Last Post: 10-28-2002, 06:33 PM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. unique randomly generated numbers
    By grobe59 in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2001, 08:26 PM