Python Convert UUID 32 character hexadecimal string to “YouTube style” short identifier and back
Converting a UUID 32-Character Hexadecimal String to a “YouTube-Style” Short Identifier and Back Again in Python
In this article, we’ll show you how to use Python to convert a UUID (Universally Unique Identifier) 32-character hexadecimal string to a “YouTube-Style” short identifier, and also how to convert the short identifier back to the original UUID string.
Read more: Python Tutorial
What is a UUID?
A UUID is a 128-bit globally unique identifier, commonly used to ensure that generated identifiers are unique worldwide. In Python, you can use the uuid
module to generate and manipulate UUIDs.
Converting a UUID to a Short Identifier
In some scenarios, you may want to convert a UUID to a shorter, more readable identifier for use in URLs or display to users. Here, we’ll describe a method for converting a UUID to a “YouTube-style” short identifier.
How the Algorithm Works
We’ll use an algorithm similar to Base64 encoding to convert the identifier. This algorithm converts the 32-character hexadecimal string of the UUID into a relatively short string. The algorithm steps are as follows:
- Convert the UUID string to an integer.
- Convert the integer to Base64 encoding.
- Apply padding as needed to ensure the short identifier is of a fixed length.
Sample Code
Here is a Python function for converting a UUID string to a short identifier:
import base64
import uuid
def uuid_to_short_id(uuid_str):
# Convert the UUID string to an integer
uuid_int = int(uuid_str.replace("-", ""), 16)
# Convert the integer to Base64 encoding
short_id = base64.urlsafe_b64encode(uuid_int.to_bytes(16, "big")).rstrip(b"=")
return short_id.decode()
# Example Usage
uuid_str = "aa67aae3e44449eeb25b83aeb9b52aab"
short_id = uuid_to_short_id(uuid_str)
print(short_id)
Running the above code will output the short identifier:
qhXqo-OERJ7i1m6vrJSqrw
Customizing Short Identifier Length
By default, the short identifier generated by the above algorithm is variable in length, depending on the UUID value. If you prefer a fixed-length short identifier, you can pad it after the conversion.
Below is the modified example code:
import base64
import uuid
def uuid_to_short_id(uuid_str, length=11):
# Convert the UUID string to an integer
uuid_int = int(uuid_str.replace("-", ""), 16)
# Convert the integer to Base64 encoding
short_id = base64.urlsafe_b64encode(uuid_int.to_bytes(16, "big")).rstrip(b"=")
# Pad the short identifier to the specified length
padding_length = max(length - len(short_id), 0)
short_id += b"=" * padding_length
return short_id[:length].decode()
# Example usage
uuid_str = "aa67aae3e44449eeb25b83aeb9b52aab"
short_id = uuid_to_short_id(uuid_str, 8)
print(short_id)
This will generate a short identifier of length 8:
qhXqo-OK
Reverse Converting a Short Identifier to a UUID
Once we have generated a short identifier, we may need to convert it back to the original UUID string. Below is a Python function that converts a short identifier back to a UUID string.
import base64
import uuid
def short_id_to_uuid(short_id):
# Restore padding
short_id += "=" * (4 - len(short_id) % 4) # Base64 encoding padded to a multiple of 4
# Restore Base64 encoding to an integer
uuid_int = int.from_bytes(base64.urlsafe_b64decode(short_id), "big")
# Restore integer to UUID string
uuid_str = str(uuid.UUID(int=uuid_int))
return uuid_str
# Example usage
short_id = "qhXqo-OERJ7i1m6vrJSqrw"
uuid_str = short_id_to_uuid(short_id)
print(uuid_str)
Running the above code will output the original UUID string:
aa67aae3-e444-49ee-b25b-83aeb9b52aab
Summary
In this article, we demonstrated how to use Python to convert a 32-character hexadecimal UUID string into a short identifier. We also demonstrated how to convert the short identifier back to the original UUID string. By using an algorithm similar to Base64 encoding, we can convert long strings into shorter, more readable identifiers when needed. This conversion can be used to display identifiers in URLs and other scenarios. We hope you find this article helpful!