I have to convert a DWORD (unsigned long) RGBA to four int vars (R, G, B, and A)
So far, I have this function to convert the 4 ints to a DWORD:
unsigned long RGBA2DWORD(int iR, int iG, int iB, int iA)
{
return ((iA << 24) | (iR << 16) | (iG << 8) | iB);
}
How can I convert it back?
Something like
struct RGBA
{
int R, G, B, A;
};
RGBA DWORD2RGBA(unsigned long dwColor)
{
static RGBA tmp;
//.......conversion process
return tmp;
}
Any kind of help would be appreciated! 🙂
Thanks
If I were you, I’d stick with multiplicative-additive operations in the packing/unpacking functions. Something like this
with a symmetrical unpacking function
Note that there’s only one “magic constant” in the whole code.
Of course, if you have an external specification that is written in terms of bit patterns in the packed data, a version based on bit and shift opertions might be preferrable. Still
has much less “magic constants”.
Now you can wrap the repetivie actions/subexpressions in macros or, better, inline functions and arrive at very compact and readable packer/unpacker.