Home > Software design >  How to convert a byte type data to UUID in python
How to convert a byte type data to UUID in python

Time:11-05

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 [279556f2-7ac1-11e9-8f93-8d3245345393]

CodePudding user response:

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

CodePudding user response:

May be the question is incomplete for it has been down voted, yet here is what I tried!

def prettyPrintUUID(uuidInfo):
uuid_array = [int(x) for x in uuidInfo]
uuid_array.reverse()
uuid_array = ''.join(f'{num:02x}' for num in uuid_array)
if(len(uuid_array)==32):
    return uuid_array[:8]   '-'   uuid_array[8:12]   '-'   uuid_array[12:16]   '-'   uuid_array[16:20]   '-'   uuid_array[20:]
else:
    return uuid_array
  • Related