# Creating a unique short ID from string value

Stack Overflow Asked by datta on November 27, 2020

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.

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.

Correct answer by superb rain on November 27, 2020

As you are dealing with coordinates, I would try my best to keep the information in the final 12-digit ID.

• If your points are global, it might be necessary to keep the degrees but they may be widespread, so you can sacrifice some information when it comes to precision.
• If your points are local (all within a range of less than 10 degrees) you might skip the first two digits of the degrees and focus on the decimals.
• As it may be possible that two points are close to each other, it may be prudent to reserve one digit as a serial number.

s = "123.45678N123.45678E"
ident = "".join([s[0:6],s[10:16]]).replace(".","")
q = 0
if s[9]=="N":
q+=1
if s[-1]=="E":
q+=2
ident+=str(q)+'0'


The example would translate to 123451234530.

After computing the initial ident numbers for each ID, you should loop through them and increment the last digit if an ident is already taken.

This way you could easily reconstruct the location from the ID by just separating the first 10 digits to two degrees of the format ddd.dd and use the [-2] digit as an indicator of the quadrant (0:SW, 1:SE, 2:NW, 3:NE).

Answered by Martin Wettstein on November 27, 2020

## Related Questions

### Mock library for Python, return value only once in mocked function?

2  Asked on December 27, 2021

### Invalid shorthand property initializer

5  Asked on December 27, 2021 by pallab-ganguly

1  Asked on December 27, 2021 by vijayanand-premnath

### How can I plot more 10k points using matplotlib?

1  Asked on December 27, 2021 by andkot

### PHP; time conversion form distance and speed

2  Asked on December 27, 2021 by nway

### Jest test with SpyOn

1  Asked on December 27, 2021 by user9855821

### json.Decoding a TCP connection and logging the original data at the same time in Go

2  Asked on December 27, 2021 by mnv3akndilyweipfg0ocvwiuob

### How can i get pdf files from FTP server as base64 encoding format on Java spring mvc project?

1  Asked on December 27, 2021 by ozdenur

### Check lists are equal whilst accounting for nulls

1  Asked on December 27, 2021

### Is there a better way to access the ChildNodes in my XML Document?

2  Asked on December 27, 2021 by kiunx3

### Not sure how to change the behavior of my CSS Grid

1  Asked on December 27, 2021 by m3hran

### Fluentd how to get source from a file by executing a script

1  Asked on December 27, 2021 by snoogybunny

### Var keyword in Java

5  Asked on December 27, 2021 by harold-ibouanga

### shorten the string to the desired values

3  Asked on December 27, 2021

### LoadLibrary in C# shows annoying error dialog using GetErrorLast

1  Asked on December 27, 2021 by borckla

### electron-build fails with ERR_ELECTRON_BUILDER_CANNOT_EXECUTE

2  Asked on December 27, 2021

### opencv can’t open file: /storage/emulated/0/test.mp4 in virtual void cv::videostab::{anonymous}::VideoFileSourceImpl::reset()

0  Asked on December 27, 2021 by mrhuang

### Test data gives “Found 0 images in 0 classes” using flow_from_directory

1  Asked on December 27, 2021 by umair-nasir

### AWS notebook instance boto3 permission denied when reading from s3

1  Asked on December 27, 2021

### HTML5/CSS: Fix position of elements regardless of webpage size

2  Asked on December 25, 2021 by r656