cstringhexbluetooth-lowenergynrf51

Data received from BLE unable to convert from hex to string (Nordic)


In the on_ble_evt(ble_evt_t*) function from the ble_app_template, I added a case of 'BLE_GATTS_EVT_WRITE'. In it has the following code:

    case BLE_GATTS_EVT_WRITE:
    { 
        ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
        SEGGER_RTT_printf(0, "Length = %d \n", p_evt_write->len);
        int n;
        char* buf2 = "";    
        char* buf3 = "";    
        count += p_evt_write->len;
        for(n=0; n<p_evt_write->len; n++)
        {
                   SEGGER_RTT_printf(0, "Received[%d] : %X \n", n, p_evt_write->data[n]);
                   SEGGER_RTT_printf(0, "Received[%d] : %d \n", n, p_evt_write->data[n]);
                   if(n>0){                                                     
                       sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));    
                       strcat(buf3, buf2);
                   }                                                    
                   else{                                                
                       sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));   
                       strcpy(buf3, buf2);
                   }
                   SEGGER_RTT_printf(0, "buf2 string: %s \n", buf2);
                   SEGGER_RTT_printf(0, "buf2 hex: %X \n", buf2[0]);
                   SEGGER_RTT_printf(0, "buf3 in string: %s \n", buf3);
                   SEGGER_RTT_printf(0, "count: %d \n", count);
        }
   }

I am receiving hex values from BLE stored in p_evt_write->data[n]. I want to concatenate all these received hex values into a string, storing it in 'buf2'.

However I am receiving errors at the line of sprintf. When I put as "%X" at sprintf, the values are not converted into a string as buf2/buf3 string does not print anything. The current code of

sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));

returns an error of 'error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]'. If I removed the (char*) typecast, it returns an error of 'error: format '%s' expects argument of type 'char *''.

I saw that p_evt_write->data[n] where data is declared as

uint8_t                     data[1];

I am currently using the nRF51DK of Nordic with Eclipse IDE and SEGGER RTT JLink for debugging. All printing is printed to SEGGER RTT. (printf does not work, using SEGGER_RTT_printf to print instead)

How can I successfully concatenate all the hex values together to form a string? Thank you.


Solution

  • To append to a buffer, you can use the following:

    size_t total_len = 0;
    uint8_t buf = NULL;
    
    while (receive()) {
        size_t len = p_evt_write->len;
        buf = realloc(buf, total_len + len);
        memcpy(buf + total_len, p_evt_write->data, len);
        total_len += len;
    }
    

    To convert an array of uint8_t into a NUL-terminated string, allocate enough space, copy the bytes and append a NUL.

    char* s = malloc(total_len + 1);
    memcpy(s, buf, total_len);
    s[total_len] = 0;
    

    Of course, if any element of buf is a NUL (0), you will appear to have a truncated string. There's no getting around that (without getting rid of the NULs).


    Note that ->data doesn't contain "hex values". Hex is a text representation of a number. You don't have hex; you have numbers. That's great, because that means no conversion is needed. You just need to copy the bytes and add a NUL.

    Note that the specific error you were getting is because you were casting a uint8_t (a number) into char* (a pointer). That makes no sense. You were perhaps going for (char*)(&(p_evt_write->data[n])), but that wouldn't work either because %s doesn't simply expect a pointer to a character but a pointer to the first character of a sequence ending with a NUL character.