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

  • Home
  • SEARCH
  • 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 980133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:19:53+00:00 2026-05-16T04:19:53+00:00

I have a stream of bytes that is being read in from a socket

  • 0

I have a stream of bytes that is being read in from a socket (little endian).
Can someone tell me why only the last one of the methods below gives the correct answer?
I suspect it’s to do with the carry bit but not sure. I’ve always found that when
printing binary data in hex form.

e.g.

printf("%02X", data);

it sometimes prints out funny values with 0xff in front. The way to fix it seems to
be doing this. This still occasionally happens when data is a char data type too:

printf("%02X", data & 0xff);

Heres a simplified example from what I was seeing with my byte stream.
Where bytes would be the stream of bytes I’ve read from the socket.

int main(int argc, char* argv[])
{
    union {
        unsigned int num;
        char bytes[4];
    } x;

    x.num = 500;
    printf("x.num=%u\n", x.num);

    unsigned int method1 = x.bytes[0] | (x.bytes[1] << 8) | (x.bytes[2] << 16) | (x.bytes[3] << 24);
    printf("method1 = %u\n", method1);

    unsigned int method2 = x.bytes[0] + (x.bytes[1] << 8) + (x.bytes[2] << 16) + (x.bytes[3] << 24);
    printf("method2 = %u\n", method2);

    unsigned int method3 = (x.bytes[0] & 0xff | (x.bytes[1] & 0xff) << 8 
                            | (x.bytes[2] & 0xff) << 16 | (x.bytes[3] & 0xff) << 24);
    printf("method3 = %u\n", method3);

    return 0;
}

Which outputs:

x.num=500
method1 = 4294967284
method2 = 244
method3 = 500

Only the last extract is actually correct.
Is my method of building up the numbers the most optimal? I also tried a memcpy to a variable but again that wasn’t reliable.

  • 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-16T04:19:54+00:00Added an answer on May 16, 2026 at 4:19 am

    When signed datatypes are being cast into higher datatypes, the most significant bit is used as a sign bit. You should have unsigned char in your union. In your case 500 = 256 + 244 = 0x1f4 and the byte 244 has the most significant bit set, so when promoted becomes 0xfffffff4.

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

Sidebar

Related Questions

I have response stream from a ftp web request that returns binary file. I
I have a JSON stream being generated by a server side C++ program that
I have some SerialPort code that constantly needs to read data from a serial
I have a byte stream that may be UTF-8 data or it may be
I have an API call that returns a byte array. I currently stream the
I have a stream of bytes which contains a flag which identifies the endianness
I have a stream of data that I want to place into a container.
I have a stream of data coming in over the serial line from an
In a project I have a stream for users, everyone can share and comment
I'm trying to read the response stream from an HttpWebResponse object. I know the

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.