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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:24:50+00:00 2026-06-15T14:24:50+00:00

I was wondering if system endianness matters when converting a byte array to a

  • 0

I was wondering if system endianness matters when converting a byte array to a short / int / long. Would this be incorrect to do if the code runs on both big-endian and little-endian machines?

short s = (b[0] << 8) | (b[1]);
int i = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3])
  • 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-15T14:24:51+00:00Added an answer on June 15, 2026 at 2:24 pm

    Yes, endianness matters. In little endian you have the most significant byte in the upper part of the short or int – i.e. bits 8-15 for short and 24-31 for int. For big endian the byte order would need to be reversed:

    short s = ((b[1] << 8) | b[0]);
    int i = (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
    

    Note that this assumes that the byte array is in little endian order. Endianness and conversion between byte array and integer types depends not only on the endianness of the CPU but also on the endianness of the byte array data.

    It is recommended to wrap these conversions in functions that will know (either via compilation flags or at run time) the endianness of the system and perform the conversion correctly.

    In addition, creating a standard for the byte array data (always big endian, for example) and then using the socket ntoh_s and ntoh_l will offload the decision regarding endianness to the OS socket implementation that is aware of such things. Note that the default network order is big endian (the n in ntoh_x), so having the byte array data as big endian would be the most straight forward way to do this.

    As pointed out by the OP (@Mike), boost also provides endianness conversion functions.

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

Sidebar

Related Questions

Trying to do a page system, and wondering if this code is valid... $get
I'm just wondering on how would you check java codes like System.Out.println(hi); it should
I'm just wondering why passing a System.Collections.Generic.List<string> into this function test(ICollection<object> t) will not
I am wondering about the the following piece of Java code: System.out.println. I am
I am looking into the ASP.NET MVC4 System.Web.Optimization bundling and was wondering how would
I am wondering where to get the javaScript system library's source code? I need
I was wondering what would be the best strategy to implement a badges system
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I'm wondering which collection-type in System.Collections.Generic will support this scenario, if there is one:
I was wondering how to change my navigation system between pages on a w8

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.