Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:56:56+00:00 2026-06-17T21:56:56+00:00

I have the example : unsigned int dwColor = 0xAABBCCFF; //Light blue color And

  • 0

I have the example :

unsigned int dwColor = 0xAABBCCFF; //Light blue color
  • And its parameters from left to right are : “alpha, red, green, blue”; each parameter requires two hexadecimal values.

  • The maximum value of each parameter is 255; lowest : 0

And, how to extract then convert all parameters of a DWORD color to decimals?

I like the value range “0.00 -> 1.00”.
For example :

float alpha = convert_to_decimal(0xAA); //It gives 0.666f
float red = convert_to_decimal(0xBB); //It gives 0.733f
float green = convert_to_decimal(0xCC); //It gives 0.800f
float blue = convert_to_decimal(0xFF); //It gives 1.000f

EDIT : I’ve just seen union, but the answerer says it’s UB (Undefined Behaviour). Does anyone know the better solution? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:56:58+00:00Added an answer on June 17, 2026 at 9:56 pm

    I usually use an union:

    union color
    {
        unsigned int value;
        unsigned char component[4];
    };
    
    color c;
    c.value = 0xAABBCCFF;
    unsigned char r = c.component[0];
    unsigned char g = c.component[1];
    unsigned char b = c.component[2];
    unsigned char a = c.component[3];
    

    If you need to treat it as a float value:

    float fr = c.component[0] / 255.0f;
    float fg = c.component[1] / 255.0f;
    float fb = c.component[2] / 255.0f;
    float fa = c.component[3] / 255.0f;
    

    EDIT:

    As mentioned in the comments below, this use of union is Undefined Behaviour (UB), see this question from Luchian Grigore.


    EDIT 2:

    So, another way to break a DWORD into components avoiding the union is using some bitwise magic:

    #define GET_COMPONENT(color, index) (((0xFF << (index * 8)) & color) >> (index * 8))
    

    But I do not advise the macro solution, I think is better to use a function:

    unsigned int get_component(unsigned int color, unsigned int index)
    {
        const unsigned int shift = index * 8;
        const unsigned int mask = 0xFF << shift;
        return (color & mask) >> shift;
    }
    

    How it works? Lets supose we call get_component(0xAABBCCFF, 0):

    shift = 0 * 8
    shift = 0
    
    mask = 0xFF << 0
    mask = 0x000000FF
    
    0x000000FF &
    0xAABBCCFF
    ----------
    0x000000FF
    
    0x000000FF >> 0 = 0xFF
    

    Lets supose we call get_component(0xAABBCCFF, 2):

    shift = 2 * 8
    shift = 16
    
    mask = 0xFF << 16
    mask = 0x00FF0000
    
    0x00FF0000 &
    0xAABBCCFF
    ----------
    0x00BB0000
    
    0x00BB0000 >> 16 = 0xBB
    

    Warning! not all color formats will match that pattern!

    But IMHO, the neater solution is to combine the function with an enum, since we’re working with a limited pack of values for the index:

    enum color_component
    {
        A,B,G,R
    };
    
    unsigned int get_component(unsigned int color, color_component component)
    {
        switch (component)
        {
            case R:
            case G:
            case B:
            case A:
            {
                const unsigned int shift = component * 8;
                const unsigned int mask = 0xFF << shift;
                return (color & mask) >> shift;            
            }
    
            default:
                throw std::invalid_argument("invalid color component");
        }
    
        return 0;
    }
    

    The last approach ensures that the bitwise operations will only be performed if the input parameters are valid, this would be an example of usage:

    std::cout
        << "R: " << get_component(the_color, R) / 255.0f << '\n'
        << "G: " << get_component(the_color, G) / 255.0f << '\n'
        << "B: " << get_component(the_color, B) / 255.0f << '\n'
        << "A: " << get_component(the_color, A) / 255.0f << '\n';
    

    And here is a live demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, If I have, int a = 42; unsigned b = 10; int
Here I have an example class : template<typename T, unsigned int SIZE> class MyClass
For example, I have the following table: CREATE TABLE `test` ( `total_results` int(10) unsigned
I have to convert a binary number like for example unsigned int bin_number =
I have a double converted to a unsigned char array. For example for value
I have the following for loop: ofstream myfile; myfile.open (proc.txt, ios::app); for (unsigned int
Say I have a float that I want to interpret as an unsigned int
I have a template: template<unsigned int N> struct IntN; template <> struct IntN< 8>
Sometimes I have to convert from an unsigned integer value to a float. For
I have this code: void fill_array (unsigned int *iarray, char *string, int max) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.