I have a short integer variable called s_int that holds value = 2
unsighed short s_int = 2;
I want to copy this number to a char array to the first and second position of a char array.
Let’s say we have char buffer[10];. We want the two bytes of s_int to be copied at buffer[0] and buffer[1].
How can I do it?
The usual way to do this would be with the bitwise operators to slice and dice it, a byte at a time:
though this should really be done into an
unsigned char, not a plaincharas they are signed on most systems.Storing larger integers can be done in a similar way, or with a loop.