Operating System - HP-UX
1840613 Members
3508 Online
110166 Solutions
New Discussion

library function to generate unique number for any string

 
Amit Kureel
Advisor

library function to generate unique number for any string

Is there a library function to generate a unique number for any string that I get as input. Also, it should generate the same number if I pass the same string to it again and again !

Can anybody help ?

Thanks and regards

Amit Kureel
4 REPLIES 4
Andreas Voss
Honored Contributor

Re: library function to generate unique number for any string

Hi,

i know no library function but how about this:

int uniq(str)
char *str;
{
int i,l,n;

for(n=0,i=0,l=strlen(str);i n+=(int)str[i];
return(n);
}

Regards
Volker Borowski
Honored Contributor

Re: library function to generate unique number for any string

Amit,

I think this is not possible at all.
Any checksum-algorithm might produce the same number for same strings again, but it will never be unique, because due to truncation at a given point, two diffrent strings might return the same number!

Try "man cksum" for a sample.

You could write an algorithm if you limit the amount of characters user in the given string and the maximum length. But this would go closer to some kind of compression.

I do not know, what you want to achieve, but I think the compression algorithms might be close to what you want. The problem is, that you will get a compressed string of a shorter length as a result, and not a simple number.

Hope this helps
Volker


Mike Stroyan
Honored Contributor

Re: library function to generate unique number for any string

You want XrmStringToQuark from libX11. (The manual does not mention it, but you should use
#include
to get the function declaration.)

The function uses a hash table to implement the mapping. It keeps a copy of each string to verify uniqueness. You can get a string back with XrmQuarkToString.
Wodisch
Honored Contributor

Re: library function to generate unique number for any string

Hello Amit,

you could use UUID (DCE Universal Unique IDs) in one
way, an array to store them and the appropriate string
for the way back. UUIDs are 128bits in size, and the
creation of them is guaranteed to be unique...

HTH,
Wodisch