C programming question

Discussion in 'Programmer's Corner' started by Art, Jan 20, 2009.

  1. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    Hi Guys,
    Have a small problem.
    How can I get four values out of a 32 bit variable into four seperate bytes (or u8 vars)
    like this:

    32bitvar = 11223344;

    do something here

    u8a = 11;
    u8b = 22;
    u8c = 33;
    u8d = 44;


    assistance would help me out of a jam!
    I'm essentially trying to split a 32bit pixel value into seperate alpha, red, green & blue values.
     
  2. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Code ( (Unknown Language)):
    1.  
    2. bigVar = 11223344;
    3.  
    4. byteD = (uchar)(bigVar & 0xFF);
    5.  
    6. bigVar >>= 0x10;
    7.  
    8. /* Continue with byteC, byteB,and byteA */
    9.  
    Now whether you continue linearly, or build a loop is up to you.

    --Rich
     
Loading...