Search code examples
pythonhashlib

Creating a unique short ID from string value


I have some data that has unique IDs stored as a string in the form:

ddd.dddddaddd.dddddz

Where d is some digit and a/z is some alphabet character. The digits may be 0-9 and the characters are either E or W for the a and N or S for the z.

I'd like to turn this into a unique integer and what I've tried using the hashlib module returns:

>>> int(hashlib.sha256(str.encode(s)).hexdigest(), 16)
Output: a very long integer (on another system cannot copy it)

Is there a way to generate a unique integer ID from a string so that it does not exceed 12 digits? I know that I will never need a unique integer ID beyond 12 digits.


Solution

  • Just something simple:

    >>> s = '123.45678W123.45678S'
    >>> int(s.translate(str.maketrans('EWNS', '1234', '.')))
    123456782123456784
    

    Not the impossible 12 digits you're still asking for in the question, but under the 20 digits you allowed in the comments.