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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:35:01+00:00 2026-05-16T08:35:01+00:00

I have defined the following struct to represent an IPv4 header (up until the

  • 0

I have defined the following struct to represent an IPv4 header (up until the options field):

struct IPv4Header
{
    // First row in diagram
    u_int32 Version:4;
    u_int32 InternetHeaderLength:4;     // Header length is expressed in units of 32 bits.
    u_int32 TypeOfService:8;
    u_int32 TotalLength:16;

    // Second row in diagram
    u_int32 Identification:16;
    u_int32 Flags:3;
    u_int32 FragmentOffset:13;

    // Third row in diagram
    u_int32 TTL:8;
    u_int32 Protocol:8;
    u_int32 HeaderChecksum:16;

    // Fourth row in diagram
    u_int32 SourceAddress:32;

    // Fifth row in diagram
    u_int32 DestinationAddress:32;
};

I now also captured an IP frame with Wireshark. As an array literal it looks like this:

// Captured with Wireshark
const u_int8 cIPHeaderSample[] = {
    0x45, 0x00, 0x05, 0x17,
    0xA7, 0xE0, 0x40, 0x00,
    0x2E, 0x06, 0x1B, 0xEA,
    0x51, 0x58, 0x25, 0x02,
    0x0A, 0x04, 0x03, 0xB9
};

My question is: How can I create a IPv4Header object using the array data?

This doesn’t work because of incompatible endianness:

IPv4Header header = *((IPv4Header*)cIPHeaderSample);

I’m aware of the functions like ntohs and ntohl, but it can’t figure out how to use them correctly:

u_int8 version = ntohs(cIPHeaderSample[0]);
printf("version: %x \n", version);

// Output is:
// version: 0

Can anyone help?

  • 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-16T08:35:02+00:00Added an answer on May 16, 2026 at 8:35 am

    The most portable way to do it is one field at a time, using memcpy() for types longer than a byte. You don’t need to worry about endianness for byte-length fields:

    uint16_t temp_u16;
    uint32_t temp_u32;
    struct IPv4Header header;
    
    header.Version = cIPHeaderSample[0] >> 4;
    
    header.InternetHeaderLength = cIPHeaderSample[0] & 0x0f;
    
    header.TypeOfServer = cIPHeaderSample[1];
    
    memcpy(&temp_u16, &cIPHeaderSample[2], 2);
    header.TotalLength = ntohs(temp_u16);
    
    memcpy(&temp_u16, &cIPHeaderSample[4], 2);
    header.Identification = ntohs(temp_u16);
    
    header.Flags = cIPHeaderSample[6] >> 5;
    
    memcpy(&temp_u16, &cIPHeaderSample[6], 2);
    header.FragmentOffset = ntohs(temp_u16) & 0x1fff;
    
    header.TTL = cIPHeaderSample[8];
    
    header.Protocol = cIPHeaderSample[9];
    
    memcpy(&temp_u16, &cIPHeaderSample[10], 2);
    header.HeaderChecksum = ntohs(temp_u16);
    
    memcpy(&temp_u32, &cIPHeaderSample[12], 4);
    header.SourceAddress = ntohl(temp_u32);
    
    memcpy(&temp_u32, &cIPHeaderSample[16], 4);
    header.DestinationAddress = ntohl(temp_u32);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read the bitmap header. I have defined the following struct: typedef
I have defined the following struct in C: typedef struct point{ float x; float
I have a struct defined as the following: typedef struct { string foo; }
I have a struct as defined by the following: typedef struct { NSString *SportName;
I have the following structure and object of structure defined in the header file
I have defined the following select list: <%= this.Select(Scope) .Options(Scopes.AllValues) // static property to
I have two struct s defined as in the following: struct vertex { double
I have the following struct defined in a user control: public struct ColumnData {
Why I'm asking this is because following happens: Defined in header: typedef struct PID
I have a function f , defined as following: struct s { void *data;

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.