简体   繁体   English

如何将字符串(字符数组)中的UUID转换为静态uint8_t adv_data [31]

[英]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: 我有一个像这样的"3bdb098f-b8b0-4d1b-baa2-0d93eb7169c4"的iBeacon UUID,我需要它看起来非常像:

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) 我需要一种在代码中转换它的方法,但是一种“手动”转换这种方法的方法也很酷(加上arduino代码不必每次都处理转换)

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. 将数组中的“current”元素设置为值。 Set "current" to the next element in the array. 将“current”设置为数组中的下一个元素。

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. 使用库函数转换为数字形式的替代方法,您可以简单地将字符0 - 9'a' - 10 (或简称'W' )的'0'减去字符a - f以执行转换。 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). 注意:在尝试转换以进行其他验证之前,您可以添加进一步检查uuid[ui]是有效的十六进制字符或'-' )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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