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

The Archive Base Latest Questions

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

I have a char xyz[2]; I receive 2 octet numbers in network byte order

  • 0

I have a char xyz[2];
I receive 2 octet numbers in network byte order in xyz[0] and xyz[1].How do i change this to host order.
How do i use ntohs to convert xyz. Please 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-26T21:22:15+00:00Added an answer on May 26, 2026 at 9:22 pm

    Do you mean “How do I convert a data stream in network order to a data stream in host order?” In that case you can use the ntohs()/htons() functions. Be careful how you invoke those, though, since you may have to take alignment issues into account. The universal solution would be to do a manual swapping of the (or each) pair of bytes.

    On the other hand, if you want to deserialize data that comes to you in network order, and you want to use the values that are serialized in the data in your program, then the notion of a “host order” is a red herring. All you need to know whether the data that you receive is in big-endian or little-endian order:

    unsigned short int le_value = xyz[0] + (xyz[1] << 8);   // little-endian
    unsigned short int be_value = xyz[1] + (xyz[0] << 8);   //    big-endian
    

    This is a typical hallmark of platform-independent programming: Your program interna should be entirely independent of implementation details, and fixed implementations have to be specified precisely at the program boundaries (i.e. (de)serialization).

    Note that in general, you cannot just take an existing byte buffer and interpret it as a different value in place, since that is not allowed by the standard. That is, you can only treat data as an int that has been declared as an int. For example, the following is illegal: char * buf = get(); int a = *(int*)buf;. The legal version starts with the target type: int a; get_data((char*)&a);

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

Sidebar

Related Questions

I have a structure defined as struct xyz { char *str; int x; int
Possible Duplicate: Convert from char* to NSString? I have char myChar = 'r' How
I have: char *str = abc def abcdef ghi xyz; I want to assign
I have char date[] = 2011-04-01; How it convert to timestamp in C or
For example I have char str[1024]; And I will do memset(str,0,1024); later Is this
If we have char *val = someString; and then say if(val){ .... } what
Assuming I have char C whose ascii code is 0110 0111 . How can
Assume I have char **argv . First, how can I print out all the
fclose() is causing a segfault. I have : char buffer[L_tmpnam]; char *pipeName = tmpnam(buffer);
I am using apache.commons.net.telnet . I have char[] array . I am calling TelnetClient.getOutputStream().write(array[i]).

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.