Reputation: 135
I want to change a uint8_t
array type to a int
or string
type, so i can write it in a text file.
for example array:
uint8_t outstr[4]; \\ outstr is 0x00 0x04 0x49 0xba
i tried using this code:
fprintf(ptr_myfile, "%d \n", *outstr);
this code give me only the first number '0' ,but I need the all array.
the expected result of the hex number 0x000449ba
should be 281018
.
Upvotes: 2
Views: 554
Reputation: 67475
two another ways:
typedef union
{
uint32_t u32;
uint8_t u8[4];
}u32_t;
int main()
{
u32_t u32u;
uint32_t u32;
/* -----------------------*/
memcpy(&u32, outstr, sizeof(u32));
printf("%u", u32);
/* -----------------------*/
//you can also use a loop
u32u.u8[0] = outstr[0];
u32u.u8[1] = outstr[1];
u32u.u8[2] = outstr[2];
u32u.u8[3] = outstr[3];
printf("%u", u32u.u32);
return 0;
}
Upvotes: 0
Reputation: 2813
If your array size is always 4 you could create a uint32_t
variable:
uint32_t num = 0;
for(int i=0; i<4; i++)
{
num <<= 8;
num |= outstr[i];
}
fprintf(ptr_myfile, "%" PRIu32 "\n", num);
Upvotes: 4