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 396337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:32:33+00:00 2026-05-12T16:32:33+00:00

I have to convert a DWORD (unsigned long) RGBA to four int vars (R,

  • 0

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

  • 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-05-12T16:32:33+00:00Added an answer on May 12, 2026 at 4:32 pm

    If I were you, I’d stick with multiplicative-additive operations in the packing/unpacking functions. Something like this

    unsigned long RGBA2DWORD(int iR, int iG, int iB, int iA)
    {        
      return ((iA * 256 + iR) * 256 + iG) * 256 + iB;
    }
    

    with a symmetrical unpacking function

    RGBA DWORD2RGBA(unsigned long dwColor)
    {        
      RGBA tmp; /* why did you declare it static??? */
    
      tmp.B = dwColor % 256; dwColor /= 256;
      tmp.G = dwColor % 256; dwColor /= 256;
      tmp.R = dwColor % 256; dwColor /= 256;
      tmp.A = dwColor % 256; /* dwColor /= 256; */
    
      return tmp;
    }
    

    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

    unsigned long RGBA2DWORD(int iR, int iG, int iB, int iA)
    {        
      return (((((iA << 8) + iR) << 8) + iG) << 8) + iB;
    }
    
    RGBA DWORD2RGBA(unsigned long dwColor)
    {        
      RGBA tmp; /* why did you declare it static??? */
    
      tmp.B = dwColor & 0xFF; dwColor >>= 8;
      tmp.G = dwColor & 0xFF; dwColor >>= 8;
      tmp.R = dwColor & 0xFF; dwColor >>= 8;
      tmp.A = dwColor & 0xFF; /* dwColor >>= 8; */
    
      return tmp;
    }
    

    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.

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

Sidebar

Related Questions

I have to convert bytes to signed/unsigned int or short. The methods below are
How to convert the signed DWORD to Unsigned DWORD ? I have one method
I'm trying to convert this function to JNA: DWORD WINAPI WlanHostedNetworkSetProperty( __in HANDLE hClientHandle,
I have to convert a binary number like for example unsigned int bin_number =
I have a date like this:- 20091023 i have to convert it to a
I have to convert a long string of data into values so that I
I have to convert this http post from JavaScript to Android. I encountered a
I have this code: #include stdafx.h #include <windows.h> #include <conio.h> int _tmain(int argc, _TCHAR*
I have something like this: if(GetFileAttributesW(C:\\Directory)!=INVALID_FILE_ATTRIBUTES) {...} I get error: cannot convert 'const char*'
I recently have convert my project from VB6 to VB.NET2008, after the convert here

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.