How To Convert 'uint32_t' to Char Array

-3

I want to convert 'uint32_t' variable to char array like this :

If uintt32_t is 0x00000001 than char array should be {0x30, 0x30, 0x30, 0x30, 0x30,0x30, 0x30, 0x31}

If uintt32_t is 0x12345678 than char array should be {0x31, 0x32, 0x33, 0x34, 0x35,0x36, 0x37, 0x38}

For now I'm using this code & it has a problem.

char myStr[12] = {0x0, 0x0, 0x0, 0x0, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
uint32_t rs = getMyInt();
sprintf(myStr, "%08jX\n", rs);

This code converts 0x12345678 to {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x00, 0x00, 0x00, 0x00} which is OK to me.

But it converts 0x01111111 to {0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x46, 0x46, 0x31, 0x42, 0x30} which is not OK.

It looks like if uint32_t value has preceeding '0's this code not working properly.

How can I fix this?

c
asked on Stack Overflow Nov 3, 2020 by Trax

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0