It's the exact opposite, cast to a long and shift left:
Code:
unsigned char RR=0x5a, GG=0, BB=0x60;
unsigned long RGBval = ((unsigned long)RR)<<16 | ((unsigned long)GG)<<8 | (unsigned long)BB;
// or
unsigned long RGBval2=RR;
RGBval2=(RGBval2<<8)|GG;
RGBval2=(RGBval2<<8)|BB;
For splitting out the individual components, unsigned char may not be the best choice to use if you're calculating the average; you're likely to overflow those 8 bits.
So you can keep it all as longs just by shifting and ANDing, e.g.
Code:
unsigned long RGBval1=0x5a0060;
unsigned long RGBval2=(RGBval1>>16) & 0xff;
Then RGBval2 contains just the red component and leaves you loads of headroom for any calculations you want to perform.