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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:24:36+00:00 2026-06-13T03:24:36+00:00

I have a c program below, I would like to send out a 32

  • 0

I have a c program below, I would like to send out a 32 bit message in a particular order Eg.0x00000001.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>

struct  test
{
    uint16_t a;
    uint16_t b;
};

int main(int argc, char const *argv[])
{
    char buf[4];
    struct test* ptr=(struct test*)buf;
    ptr->a=0x0000;
    ptr->b=0x0001;
    printf("%x %x\n",buf[0],buf[1]); //output is 0 0
    printf("%x %x\n",buf[2],buf[3]); //output is 1 0
    return 0;
}

Then I test it by print out the values in char array. I got output in the above comments. Shouldn’t the output be 0 0 and 0 1? since but[3] is the last byte? Is there anything I missed?

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-06-13T03:24:37+00:00Added an answer on June 13, 2026 at 3:24 am

    Cause its little endian. Read this:
    Endianness

    For translating them into network order, you have to use htonl (host-to-network-long) and htons (host-to-network-short) translating functions. After you receive, you need to use ntohl and ntohs functions to convert from network to host order. The order which your bytes are placed in the array depends on the way how did you put them in memory. If you put them as four separate short bytes, you will omit endianness conversion. You may use char type for this kind of raw bytes manipulations.

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

Sidebar

Related Questions

I have a C program like the one below. I am trying to write
I have a program that I would like to run on just one CPU
I have a 3rd-party installer program that I would like for my users to
I have the following code shown below in my program. One of the message
In a C++ program, I would like to have a long-only option with a
I have a program shown below: Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); EndPoint
I have the below program that will move files from one directory to other.
I have a Enumeration as shown in below program public class Test { public
I have a little confusion in the following two statements. The below program is
Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates

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.