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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:46:57+00:00 2026-05-22T21:46:57+00:00

I have a struct that’s used in my internet application, which looks like: struct

  • 0

I have a struct that’s used in my internet application, which looks like:

struct query {
     ushort trans_id;
     ushort flags;
     ushort opcode;
     ushort content_length;
     char content[512];
};

Basically, I’m just trying to take this structure and turn it into a “packet” to be sent. It must have the corresponding length and everything. So; if I could I would have like:
struct query q;

q.trans_id = 0x5544;
q.flags = 0x0000;
q.opcode = 0x0010;
q.content_length = 0x0001;
q.content[0] = '\0'; //I would want my packet to end up looking like this, when outputted to the socket:
//\x55\x44\x00\x00\x00\x10\x00\x01\x00
//and yes, it's going to be stored in a char buffer, but I'm not using any functions that will trim off any of the things past the null!

I feel like it’s a relatively simple question that can probably be answered by casting my data types, but I think it’s a bit harder than that.

I also did try using sprintf, but it would turn my nulls into actual 0s, so 0x0000 would become 2 ‘0’ ASCII characters!

  • 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-22T21:46:57+00:00Added an answer on May 22, 2026 at 9:46 pm

    You can indeed simply cast the pointer to your struct to a char *, such as:

    #include <stdlib.h> /* for size_t */
    
    ....
    
    struct query q;
    /* fill in the struct ... */
    char *packet = (char *)(&q);
    size_t packet_size = sizeof(struct query);
    

    If you want the packet size to trim the content of the query struct, you’ll have to calculate the difference (by using strnlen for example on the content and subtracting the difference)

    EDIT: Alternatively for trimming you can do:

    int packet_size = (&(q.content[0]) + strnlen(q.content, 512)) - &q;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
Say I have a struct that looks like this (a POD): struct Foo {
In my C# application, I have a large struct (176 bytes) that is passed
I have a struct that I initialize like this: typedef struct { word w;
I have a struct that I'd like to output using either 'std::cout' or some
I have a struct called Point (which happens to be a Python Extension) that
I have a C (not C++) struct that goes like this typedef struct mystruct{
I have a function that takes a struct, and I'm trying to store its
I have this routine that calculates the seconds-to-date for a struct tm . On
I have a function that takes pointer to pointer to struct. struct ABC; void

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.