|
-
January 7th, 2010, 10:13 PM
#1
Request an algorithm, convert a string to a unique int
I need to convert a string to a unique integer, and better it's thread safe. Is that possible? How to?
Thanks.
-
January 7th, 2010, 10:48 PM
#2
Re: Request an algorithm, convert a string to a unique int
I assume you mean convert a string to an int?
well there are multiple ways http://www.codeguru.com/forum/showthread.php?t=231054
I don't know how thread safe those are. If you want a thread safe conversion, just build one. it isn't hard, if it doesn't have floating precision.
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
January 8th, 2010, 12:43 AM
#3
Re: Request an algorithm, convert a string to a unique int
thanks, but I didn't think that work for my case.
I mean "unique", that means the resulting integer should be unique from different strings, even from different character sequence.
I know it may be impossible, since length of string is limitless, however, integer gets maximum value.
-
January 8th, 2010, 01:55 AM
#4
Re: Request an algorithm, convert a string to a unique int
I don't understand what you mean by unique. Maybe you can explain more or show an example in Pseudocode.
someone else might know what you're asking
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
January 8th, 2010, 05:49 AM
#5
Re: Request an algorithm, convert a string to a unique int
 Originally Posted by Joeman
I don't understand what you mean by unique. Maybe you can explain more or show an example in Pseudocode.
someone else might know what you're asking
I mean that convert a string to an int, which would be unique, different from other ints generated by different strings
-
January 8th, 2010, 05:50 AM
#6
Re: Request an algorithm, convert a string to a unique int
 Originally Posted by LifeIsSuffering
thanks, but I didn't think that work for my case.
I mean "unique", that means the resulting integer should be unique from different strings, even from different character sequence.
I know it may be impossible, since length of string is limitless, however, integer gets maximum value.
Maybe if you tell us what you're trying to really achieve by doing this, then maybe you will get other alternatives from persons here. What is your real goal here?
Regards,
Paul McKenzie
-
January 8th, 2010, 05:34 PM
#7
Re: Request an algorithm, convert a string to a unique int
It is impossible have a unique key for all strings.
Just sum all the values of string's elements (as char), or multiply, or 1st + 2nd*10 + 3rd*100 + 4th*1000... (I mean first second third as the strings first char second char when you think them as a char array. Of course starting with zero)
-
January 8th, 2010, 05:43 PM
#8
Re: Request an algorithm, convert a string to a unique int
Are you looking to convert the string "12345" into the integer 12345, or are you simply trying to come up with an arbitrary integer for the strong "ABC" (eg, a hash function)?
If the latter, consider: There are at most 2^32-1 possibly ints (unless you use a BigInt library). There are more possible strings (bounded only by the memory of the machine, really). So by the pigeonhole principal, it's impossible to assign a different integer to each string.
However, you can design a function which assigns distinct integers to distinct strings with high probability, which is what you'd do when designing a hash function, for instance.
-
January 9th, 2010, 04:27 AM
#9
Re: Request an algorithm, convert a string to a unique int
What you are looking for is a hash functions. There are cryptographic hash functions which have a very high probability that a string is unique (very high meaning e.g. 2^80 in the case of SHA-1). Either use one of the common hash functions (e.g. SHA-1, MD5) or google for hash algorithms, there are plenty of simpler ones.
Last edited by treuss; January 9th, 2010 at 04:31 AM.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
January 10th, 2010, 09:21 PM
#10
Re: Request an algorithm, convert a string to a unique int
thanks Lindley and treuss, a hash algorithm may be what I'm looking for...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|