Search code examples
pythonuuid

How to convert a byte type data to UUID in python


I am reading the UUID from my board as b"\x93S4E2\x8d\x9e\x8f\xe9\x11\xc1z\xd0U\x95'"

How to convert or format this to obtain a 128-bit UUID that reads [279555d0-7ac1-11e9-8f93-8d3245345393]


Solution

  • The Python UUID library will normally cover most of the situations that you need for UUIDs.

    https://docs.python.org/3/library/uuid.html

    The result I'm get doesn't match your expected output 100% but I wonder if you have a typo in your question.

    This is what I did:

    import uuid
    
    raw_id = b"\x93S4E2\x8d\x9e\x8f\xe9\x11\xc1z\xd0U\x95'"
    dev_uuid = uuid.UUID(int=int.from_bytes(raw_id, 'little'))
    print(f"Device UUID = {dev_uuid}")
    

    Which gave the output:

    Device UUID = 279555d0-7ac1-11e9-8f9e-8d3245345393