C # how to convert string to unique identifier

suppose we have string name = "stackoverflow.com";

how to convert this int string to a unique identifier or some kind of hash. (no md5 because it is too big) and it should not be random

I like to have something like this

Please note that the string itself is too large. I would like to know if a line can be written shorter. in a combination of letters, numbers and symbols

f¤k ^ § ~ 7d? Æ

+3
source share
4 answers

This is not possible without restricting your domain. There is infinitely many string and therefore cannot be mapped injectively to any finite set. Therefore, uniqueness is impossible.

If you really need a unique identifier for string , use string .

+6
source share

If you use a hash, it should be long enough to be unique and probably longer than you want. You need 2^(BitLength/2) >> n with BitLength being the hash length and n the number of lines.

How to use only Dictinary<string,int> and counter?

+1
source share

name.GetHashCode ()

this is probably the best choice. This is a common problem with any form of hash that cannot be garenteed unique, but you can make it much more likely to be unique, allowing the hash to be longer.

You can also use different hashing algorithms in combination with each other to increase the supported range.

EDIT

Then you can create a custom hashcode function like

 public static int GetHashCode (string value ) { int h = 0; for (int i = 0; i < value.Length; i ++) h += value [i] * 31 ^ value.Length - (i + 1); return h; } 

(Stolen from another place)

0
source share

Jason is absolutely right - you cannot create a unique hash with a finite string size that can be arbitrarily long. I submit to you that you are not looking for a hash, but rather a short string compression algorithm .

0
source share

All Articles