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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:49:07+00:00 2026-05-14T00:49:07+00:00

I am using ms c++. I am using struct like struct header { unsigned

  • 0

I am using ms c++. I am using struct like

    struct header   {
        unsigned port : 16;
                unsigned destport : 16;
        unsigned not_used : 7;
        unsigned packet_length : 9;


    };
struct header HR;

here this value of header i need to put in separate char array.

i did memcpy(&REQUEST[0], &HR, sizeof(HR));

but value of packet_length is not appearing properly.

like if i am assigning HR.packet_length = 31;
i am getting -128(at fifth byte) and 15(at sixth byte).

if you can help me with this or if their is more elegant way to do this.

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-14T00:49:07+00:00Added an answer on May 14, 2026 at 12:49 am

    Sounds like the expected behaviour with your struct as you defined packet_length to be 9 bits long. So the lowest bit of its value is already within the fifth byte of the memory. Thus the value -128 you see there (as the highest bit of 1 in a signed char is interpreted as a negative value), and the value 15 is what is left in the 6th byte.

    The memory bits look like this (in reverse order, i.e. higher to lower bits):

         byte 6    |     byte 5    | ...
    0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 
     packet_length   |   not_used  | ...
    

    Note also that this approach may not be portable, as the byte order inside multibyte variables is platform dependent (see endianness).

    Update: I am not an expert in cross-platform development, neither did you tell much details about the layout of your request etc. Anyway, in this situation I would try to set the fields of the request individually instead of memcopying the struct into it. That way I could at least control the exact values of each individual field.

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

Sidebar

Related Questions

If I define a struct in C# using automatic properties like this: public struct
I get an error when I compile this code: using System; public struct Vector2
How do you encode and decode a CGPoint struct using NSCoder ?
I'm trying to map a struct definition using ctypes: struct attrl { struct attrl
Is there any advantage over using a class over a struct in cases such
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I have defined the following struct to represent an IPv4 header (up until the
Can you make a struct that behaves like one of the built-in classes where
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.

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.