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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:04:50+00:00 2026-06-02T18:04:50+00:00

I am doing networking programming in C\C++ and the assignment asks me to send

  • 0

I am doing networking programming in C\C++ and the assignment asks me to send a formatted message.

The format is below:

bits:    0.....15 16....31  
          update#  port#           
             Sever-IP

As you seen first line ask me to:

  1. Convert int into 16 bits so 2 ints make up 32 bits.

  2. Convert xxx.xxx.xxx.xxx into 32 bits.

I am using UDP, so I need get these info into a char[], and that is the hard part. In my program, I have the update# and port # as int, and server IP address as a string. How could I convert them into these bits?

Here is what I have tried:

  1. Casting them into char. But, I need get one int into two char.

  2. Convert them into a string, and then string.c_str(), but it gives me a 4 bytes pointer.

  • 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-02T18:04:51+00:00Added an answer on June 2, 2026 at 6:04 pm

    The easiest way to do this is to create a struct for your header like this:

    #pragma pack(push,1)
    
    struct header
    {
        unsigned short updateNumber;
        unsigned short port;
        unsigned long ip;
    };
    #pragma pack(pop)
    

    And use it like this:

    char *buffer = new char[sizeof(header)+sizeOfRemainingMessage];
    header *head = (header*)buffer;
    head->update = 1;
    head->port = 80;
    head->ip = // convert ip string to unsigned long*
    send(buffer); 
    

    *To convert the ip string to the unsigned long refer to inet_addr().

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

Sidebar

Related Questions

I'm doing some networking programming for Windows right now envolving the HTTP protocol. Is
I am doing networking project, My current broadband internet speed is 2Mbps my application
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
Doing some homework here (second assignment, still extremely green...). The object is to read
Doing code analysis of the project and get the message Reference-counted object is used
I am doing an social networking project ,where i have an option to add/edit
I am new to UDP/networking programming, and I am trying to create a chat
So I am doing a lot of high performance network programming using Boost::Asio (or
Hey Guys, I am doing a social networking site that requires a good bit
I am creating a social networking type website. The website is doing great in

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.