Copying bytes of array of uint_32 to unsigned char

1

I am trying to copy the bytes of state to charstate using memcpy

uint32_t state[3] = {0x00000300, 0x00001100, 0x00022200};
unsigned char charstate[1420];
memcpy(charstate, (unsigned char *)state, sizeof(uint32_t)*3);
printf("charstate: %s", charstate);

But the printf sentence does not return anything. Could you help me to copy this state array to charstate, please?

c
asked on Stack Overflow Jan 16, 2021 by Juan

1 Answer

1

You do not need help with the copying, you already succeeded.
The problem that you printf() does not output anything is caused by the format specifier "%s" printing only the characters it finds before the first 0 valued one.
Compare https://en.cppreference.com/w/c/io/fprintf

Looking at the data you copy, the first 0 value is found either in 0x00000300 or in 0x00000300, depending on the applicable endianess in your environment.
I.e. the very first value which printf("%s",...) encounters already is 0. It hence stops before outputting anything.

If the first value would not happen to be 0, you would probably not get expected output either. You would only get output at all for printable characters, which seem to be kind of rate in your data and that would probably not be what you expect either.

You probably want a hex representation of the data and Andy has proposed a way for that in the comments.

answered on Stack Overflow Jan 16, 2021 by Yunnosch

User contributions licensed under CC BY-SA 3.0