i am programming an SI4362 using an STM32. the SI Labs program outputs a header file, and for the sake of ease i would like to simply plug the header into my program however it defines an array of byte like this:
#define RF_POWER_UP 0x02, 0x01, 0x01, 0x01, 0xC9, 0xC3, 0x80
and i need to convert it to look like this:
uint8_t RF_POWER_UP[7] = { 0x02, 0x01, 0x01, 0x01, 0xC9, 0xC3, 0x80 };
i could not find anything through the search feature that made this work directly within the C program.
CodePudding user response:
The macro definition that is already being produced seems designed specifically for purposes similar to yours. You cannot easily re-use the macro name as your array's name, but you can certainly do this:
#include <stdint.h>
#include "the_generated_header.h"
uint8_t rf_power_up[] = { RF_POWER_UP };
If you do not specify the array length, as shown, then it will be made exactly long enough to accommodate all the elements of the initializer. That means you don't need to know in advance how many values will be produced (though there needs to be at least one).
CodePudding user response:
Convert a text inside of header to an array of bytes
An alternative to using the macro for initialization of a named object is to use it to define an array via a compound literal.
Use (unsigned char[]){RF_POWER_UP}
wherever you need an _array. This directly meets OP's stated goal.
#define RF_POWER_UP 0x02, 0x01, 0x01, 0x01, 0xC9, 0xC3, 0x80
printf("Size %zu\n", sizeof (unsigned char[]){RF_POWER_UP}); / prints 7.
No need for a named object:
#define RF_POWER_UP_ARRAY (unsigned char[]){RF_POWER_UP}
fwrite(RF_POWER_UP_ARRAY, sizeof RF_POWER_UP_ARRAY, 1, stream);