String to unique integer hashing

前端 未结 7 1395
日久生厌
日久生厌 2020-12-18 18:49

I\'m trying to develop a system that can change my string into a unique integral value, meaning say for example the word \"account\" has an encrypted numerical value of 0891

7条回答
  •  鱼传尺愫
    2020-12-18 19:05

    Assign a unique prime value to each alphabet in increasing order(order not necessary).

    Please Note : As multiplication of prime numbers is a unique result which can only be multiplied by these numbers, it will give you unique values for each word.

    Algorithm :

    int hash = 0;
    forEach (int i = 0 ; i < word.length ; i++)
    { 
       hash *= (prime[c[i]] ** (length - i)); 
    }
    

    prime - An array to store prime values corresponding to each

    powered to (length - 1) to give value to the place at which this character occurs to maintain a dictionary order.

    This algorithm will give sufficiently large values that will overrun your array.

    Also : words will smaller lengths may give lower values than some words with larger length and it may affect your dictionary order but I'm not sure why do you want a dictionary order as the uniqueness will be maintained here.

提交回复
热议问题