Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To: Convert String of Unknown Length into a Unique Value 1

Status
Not open for further replies.

campbelt

Programmer
Dec 8, 2007
5
US
Hello,

I am trying to figure out a way to convert a string of some unknown length into a unique value (int or string) no longer than 15 characters. Is this possible? Any thoughts or ideas?

I have been exploring various hashing algorithms and the like, btu they all produce values that are too long.

Thank you very much for your time.
 
Well, without an upper limit on the length of the string, there is potentially an infinite size. So there is no way to uniquely represent an infinite amount of data in 15 characters or less.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
The easiest thing to do would be somthing like the following:

String test = "This is a test.";
System.Console.WriteLine(test.GetHashCode().ToString());

As the previous poster noted, you can't guarantee uniqueness when mapping from a larger domain to a smaller domain. But you may take comfort that collision probabilities of two random strings are about 1/4,000,000,000... but watch out for the birthday paradox if you need to hash a very large set of data!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top