相关文章推荐
深沉的凉茶  ·  python中mqtt配置ssl_youyi ...·  1 年前    · 
刀枪不入的领结  ·  sql ...·  1 年前    · 
咆哮的黑框眼镜  ·  c# - How can I read ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm following a tutorial and it says the UUID is 65210001-28D5-4B7B-BADF-7DEE1E8D1B6D then he adds it to the code in this format, without explaining how the conversion happened:

// Simple Service UUID: 65210001-28D5-4B7B-BADF-7DEE1E8D1B6D
static struct bt_uuid_128 simple_service_uuid =
    BT_UUID_INIT_128(0x6d, 0x1b, 0x8d, 0x1e, 0xee, 0x7d, 0xdf, 0xba, 0x7b,
             0x4b, 0xd5, 0x28, 0x01, 0x00, 0x21, 0x65);

I'm curious, what format is 0x6d, 0x1b, 0x8d, 0x1e, 0xee, 0x7d, 0xdf, 0xba, 0x7b,0x4b, 0xd5, 0x28, 0x01, 0x00, 0x21, 0x65 exactly? Hex?

How can I get from the UUID to the array? I've tried hex conversions and some other python encoding, but I can't create anything close. I'm looking to do the conversion in python.

reverse 65210001-28D5-4B7B-BADF-7DEE1E8D1B6D by taking 2 at a time: 6D 1B 8D 1E EE 7D ... -> BT_UUID_INIT_128(0x6d, 0x1b, 0x8d, 0x1e, 0xee, ...) – Patrick Artner Jun 25, 2021 at 4:57 Wow, I never noticed that, thanks! I am still curious, is the hex or byte data (or something else?). @PatrickArtner labeled python because I'm trying to do the conversion in python, sorry! – exile97 Jun 25, 2021 at 4:58 0x6d is 2 byte (16 bits) expressed as hex-number (the 0x in front letters a-f in the numbers are a giveaway) - and as to why it is reversed - google for the language used and BT_IIOD_INIT_128 to get more info about the stucture used. – Patrick Artner Jun 25, 2021 at 5:00 Ok I see. I will write a python script to make the arrays for me now ;) Appreciate the help! – exile97 Jun 25, 2021 at 5:01 This really depends on how BT_UUID_INIT_128 is defined, and the struct it is applied to. Perhaps whatever library that is has documentation – M.M Jun 25, 2021 at 5:04

Here is the python solution thanks to Patrick Artners help:

    uuid = input('Enter a UUID: ')
    uuid = uuid.replace('-', '')
    uuid = uuid[::-1] #reverse the string
    hexArrayStr = ''
    splitToTwos = map(''.join, zip(*[iter(uuid)]*2))
    count = 0
    for v in splitToTwos:
        count+=1
        hexArrayStr = hexArrayStr + ('0x'+(v[::-1]).lower())
        if count != 16:
            hexArrayStr = hexArrayStr + ', '
    print(hexArrayStr)

prints 0x6d, 0x1b, 0x8d, 0x1e, 0xee, 0x7d, 0xdf, 0xba, 0x7b, 0x4b, 0xd5, 0x28, 0x01, 0x00, 0x21, 0x65

@selbie some other guy told me it shouldn't be tagged python so I removed the tag..? I'll re-add it. (See comments on answer) – exile97 Jun 25, 2021 at 5:24

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.