VBA hash string

前端 未结 4 806
无人共我
无人共我 2020-12-12 17:41

How do I get a short hash of a long string using Excel VBA

Whats given

  • Input string is not longer than 80 characters
  • Valid in
4条回答
  •  温柔的废话
    2020-12-12 18:17

    While the below is not a hash function, I've used it as a quick way to generate numeric id's that have a low collision rate over a small list (small enough to verify by inspection).

    How it Works: Column A holds the strings from row 2 onward. In row 1, A1 and B1 hold an arbitrary start and end position midway in the string. The formula uses the first letter of the string and a fixed letter taken from mid-string and uses LEN() as a 'fanning function' to reduce the chance of collisions.

     =CODE(A2)*LEN(A2) + CODE(MID(A2,$A$1,$B$1))*LEN(MID(A2,$A$1,$B$1))
    

    If strings are pulled from a database table with fixed width fields, you may need to trim the lengths:

     =CODE(TRIM(C8))*LEN(TRIM(C8))
           +CODE(MID(TRIM(C8),$A$1,1))*LEN(MID(TRIM(C8),$A$1,$B$1))
    

提交回复
热议问题