I would like to store an uint8_t (could change to uint_16 or 32 too) with their formatting in a string. I need to format them as hex, and want to store them along with their formatting. I have something like this (just the core):
uint8_t telegramData[];
for (int i = 0; i < sizeof(telegram); i++)
{
printf("Uint8_t: %02X", telegramData[i]);
}
example output: Uint8_t: C8 4A 00 0D
Instead of printing out should store, but with correct formatting. Best would be if I had result as example: string str = "C8 4A 00 0D" Is there any method for this? Really thanks in advance!
EDITED
Hello again, seems like i found out. I am using C++, sorry I think made tags mistake.I am using g++ compiler. Telegram is not relevant that much (yes it has proper size, just didnt note before here- "core code"). Used sprintf finally and this seems like to be a solution for me (again just core, because would take too much to explain every detail): Please don't bite my head off, I am not best with coding.
uint8_t telegram;
char *tmp = (char*)malloc(sizeof(telegram));
for (int i = 0; i < sizeof(telegram); i++)
{
sprintf (tmp + strlen(tmp), " %02X", telegram[i]);
}
printf ("Uint8_t: %s",tmp);
Output result: Uint8_t: C8 4A 00 0D I know it is not best solution, but most close what I need. And now it is also stored in tmp, so can use later too, not just printing out once. Thats why needed.
EDITED
Some better version:
uint8_t telegram;
char *tmp = (char*)malloc(sizeof(telegram));
int test = 0;
for (int i = 0; i < sizeof(telegram); i++)
{
test += sprintf (tmp + test, " %02X", telegram[i]);
}
printf ("Uint8_t: %s",tmp);
for C