Store unsigned int (uint8, 16, 32) in variable

1k views Asked by At

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);
3

There are 3 answers

0
BLUEPIXY On

for C

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#define uint_join(p, a, ch) do{\
    size_t type_size = sizeof(*a), size = sizeof(a)/type_size;\
    size_t i, len = 0;\
    p = malloc(size * (type_size * 2 + 1));\
    if(p)\
    for(i=0;i<size;++i){\
        switch(type_size){\
        case 1:\
            len += sprintf(p+len, (i < size -1 ? "%0*" PRIX8 "%c" : "%0*" PRIX8), type_size*2, a[i], ch);\
            break;\
        case 2:\
            len += sprintf(p+len, (i < size -1 ? "%0*" PRIX16 "%c" : "%0*" PRIX16), type_size*2, a[i], ch);\
            break;\
        case 4:\
            len += sprintf(p+len, (i < size -1 ? "%0*" PRIX32 "%c" : "%0*" PRIX32), type_size*2, a[i], ch);\
            break;\
        case 8:\
            len += sprintf(p+len, (i < size -1 ? "%0*" PRIX64 "%c" : "%0*" PRIX64), type_size*2, a[i], ch);\
            break;\
        }\
    }\
}while(0)

int main (void){
    uint8_t telegramData[] = { 0xC8, 0x4A, 0x00, 0x0D};
    char *tmp;
    uint_join(tmp, telegramData, ' ');
    printf ("Uint8_t: %s\n", tmp);
    free(tmp);
    return 0;
}
1
unwind On

Just use snprintf(), it's a library standard function for producing strings, very much like printf() except instead of printing also printing the resulting string, it's just created in memory.

0
John Dibling On

Your original question did not include a language tag, so it's unclear if you are programming in C or C++. The two are separate languages, and the best answers will be different for each language.

Although C++ shares a common ancestry with C, and sprintf is indeed part of the C++ Standard Library, if you're programming in C++ you probably should not be using sprintf or it's related functions. This family of functions is decidedly type-unsafe.

There are many type-safe ways to do this in C++, but a good place to start is using the C++ streams along with their manipulators:

std::stringstream ss;
ss << std::hex << telegramData[i];
std::string s = ss.str();

There are also type-safe alternatives in the Boost library which share a similar syntax to sprintf.