Python hex conversion



  • How to convert service = b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfb\xaa\xed\xde'

    to

    DEEDAAFB-000-0000-0000-000000000002



  • @misterlisty
    s = "".join(["%02X" % b[i] for i in range(len(service) - 1,-1,-1)])
    resulting in:
    DEEDAAFB000000000000000000000001
    sm = s[:8] + "-" + s[8:12] + "-" + s[12:16]+"-"+ s[16:20] + "-" + s[20:]
    resulting in:
    DEEDAAFB-0000-0000-0000-000000000001

    Which is however on letter longer than the string above. So check whether in the second group it is really three 0's.

    Edit: for the second step, you can also write:
    sm = "-".join([s[:8], s[8:12], s[12:16], s[16:20], s[20:]])


Log in to reply
 

Pycom on Twitter