Friday, November 26, 2021

Python UUID and bytes encode decode

It's been a while, I've ventured into the Python world, it's a new beginning.

This piece of code should visualize the UUID generated object converted into different forms and output as a string to the console


import uuid

import base64



def test_returns_uuid(the_uuid=None):
# Given
the_uuid = uuid.uuid4()
print('\nA UUID: ', the_uuid)
# Convert a UUID to a string of hex digits in standard form
print('\nAs String: ', str(the_uuid))
# Convert a UUID to a 32-character hexadecimal string
print('\nIn HEX: ', the_uuid.hex)
# as urlSafe binary format
base64_encoded = base64.urlsafe_b64encode(str(the_uuid).encode('utf-8'))
print('\nEncoded Base64', str(base64_encoded.decode('utf-8')).replace('=', ''))
print('\nDecoded Base64', str(base64.urlsafe_b64decode(base64_encoded
        .decode('utf-8')).decode('utf-8')))

 

No comments: