简体   繁体   中英

How do I convert UUID in string (char array) to static uint8_t adv_data[31]

I have an iBeacon UUID like this "3bdb098f-b8b0-4d1b-baa2-0d93eb7169c4" and I need it to look pretty much like that:

static uint8_t adv_data[31] = { 0x02,0x01,0x06, 0x1A,0xFF,0x4C,0x00,0x02,0x15,0x71,0x3d,0x00,0x00,0x50,0x3e,0x4c,0x75,0xba,0x94,0x31,0x48,0xf1,0x8d,0x94,0x1e,0x00,0x00,0x00,0x00,0xC5 };

I need either a method to convert that in code but a one off method of converting this "by hand" would be cool too (plus the arduino code wouldn't have to deal with the conversion everytime)

Red two characters at a time, convert them to a number corresponding to the hexadecimal value. Do in a loop. Skip the '-' character when you encounter it. Set the "current" element in the array to the value. Set "current" to the next element in the array.

Something like this pseudo code

while (not at end of string)
{
    char1 = get next character from string;
    char2 = get next character from string;
    value = make int from hex characters(char1, char2);
    array[current++] = value;
}

This should work for your problem

char *uuid = "3bdb098f-b8b0-4d1b-baa2-0d93eb7169c4";
static uint8_t adv_data[32];  //your uuid has 32 byte of data

// This algo will work for any size of uuid regardless where the hypen is placed
// However, you have to make sure that the destination have enough space.

int strCounter=0;      // need two counters: one for uuid string (size=38) and
int hexCounter=0;      // another one for destination adv_data (size=32)
while (i<strlen(uuid))
{
     if (uuid[strCounter] == '-') 
     {
         strCounter++;     //go to the next element
         continue;
     }

     // convert the character to string
     char str[2] = "\0";
     str[0] = uuid[strCounter];

     // convert string to int base 16
     adv_data[hexCounter]= (uint8_t)atoi(str,16);

     strCounter++;
     hexCounter++;
}

An alternative to using a library function to convert to numeric form, you can simply subtract '0' for characters 0 - 9 and 'a' - 10 (or simply 'W' ) for characters a - f to preform the conversion. For example:

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#define MAXC 32

int main (int argc, char **argv) {

    char *uuid = argc > 1 ? argv[1] : "3bdb098f-b8b0-4d1b-baa2-0d93eb7169c4";
    uint8_t data[MAXC] = {0};

    size_t i, ui = 0, di = 0, ulen = strlen (uuid);

    for (;di < MAXC && ui < ulen; ui++, di++) {
        if (uuid[ui] == '-') ui++;    /* advance past any '-'  */
                                      /* convert to lower-case */
        if ('A' <= uuid[ui] && uuid[ui] <= 'Z') uuid[ui] |= (1 << 5);
        data[di] = ('0' <= uuid[ui] && uuid[ui] <= '9') ? uuid[ui] - '0' :
                    uuid[ui] - 'W';   /* convert to uint8_t */
    }

    if (di == MAXC && ui != ulen) {  /* validate all chars fit in data */
        fprintf (stderr, "error: uuid string exceeds storage.\n");
        return 1;
    }

    printf ("\n original: %s\n unsigned: ", uuid);
    for (i = 0; i < di; i++)
        putchar (data[i] + (data[i] < 10 ? '0' : 'W'));
    putchar ('\n');

    return 0;
}

Example Output

$ ./bin/uuid_to_uint8_t

 original: 3bdb098f-b8b0-4d1b-baa2-0d93eb7169c4
 unsigned: 3bdb098fb8b04d1bbaa20d93eb7169c4

( note: you can add a further check that uuid[ui] is a valid hexadecimal character or '-' before attempting the conversion for additional validation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM