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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:22:03+00:00 2026-06-09T12:22:03+00:00

I have a four byte DWORD that I need to split into four different

  • 0

I have a four byte DWORD that I need to split into four different characters. I thought I knew how to accomplish this but am getting bizarre numbers every time. Here is my code:

    // The color memory
    int32 col = color_mem[i];

    // The four destination characters
    char r, g, b, a;

    // Copy them in advancing by one byte every time
    memcpy(&r, &col, 1);
    memcpy(&g, &col + 1, 1);
    memcpy(&b, &col + 2, 1);
    memcpy(&a, &col + 3, 1);
  • 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-09T12:22:05+00:00Added an answer on June 9, 2026 at 12:22 pm

    Ditch the memcpy and use bit manipulation:

    r = (col & 0x000000ff);
    g = (col & 0x0000ff00) >>  8;
    b = (col & 0x00ff0000) >> 16;
    a = (col & 0xff000000) >> 24;
    

    ff in the hexadecimal numbers represents a byte of all 1 bits. This and the & – bitwise AND – will make the bytes that you’re not interested in – at each position – 0, and keeping the bits that you are interested in.

    The >> shifts in zeros from the left, putting the byte that we want in the most significant position, for the actual assignment. A shift of 8 shifts by a width of one byte, 16 is two bytes, and 24 is three bytes.

    Visually, looking at ff, you can imagine that we’re walking the byte indices towards the left.

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

Sidebar

Related Questions

I have a byte array, and I need to get four bytes from it
I have four db servers with the same db structure but different data. Currently
I have to pack and unpack a Cardinal into four one-byte fields ( in
I have four divs each with the same ID but different class. <div id=inner
i have four tables user-question contains two columns: questionID, userID, the questions that the
i have four imageviews in a linear layout in such a manner that only
I have four different lists. headers , descriptions , short_descriptions and misc . I
I have a large hexidecimal (16 byte, 32 hex digits) data item that always
I have a byte buffer 6 bytes long first four contains ip address last
I need a function that takes four unsigned char variables as parameters and combines

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.