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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:54:59+00:00 2026-05-26T20:54:59+00:00

I have a binary file in big-endian format from which I am retrieving 2-bit

  • 0

I have a binary file in big-endian format from which I am retrieving 2-bit and 4-bit integer data. The machine I’m running on is little-endian.

Does anyone have any suggestions or a best-practice on pulling integer data from a known format binary and switching endianness on the fly? I’m not sure that my current solution is even correct:

int myInt;

ifstream dataFile(dataFileLocation, ios::in | ios::binary);
dataFile.seekg(99, ios::beg);  //Pull data starting at byte 100;

//For 4-byte value:
char chunk[4];
dataFile.read(chunk, 4);
myInt = (int)(chunk[0] << 24 | chunk[1] << 16 | chunk[2] << 8 | chunk[3]);

//For 2-byte value:
char chunk[2];
dataFile.read(chunk, 4);
myInt = (int)(chunk[0] << 8 | chunk[1]);

This seems to work fine for 2-byte data but gives what I believe are incorrect values on 4-byte data. I’ve read about htonl() but from what I’ve read that’s not a smart way to go for flexibility.

  • 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-26T20:54:59+00:00Added an answer on May 26, 2026 at 8:54 pm

    Use unsigned integral types only and you’ll be fine:

    unsigned char buf[4];
    infile.read(reinterpret_cast<char*>(buf), 4);
    
    unsigned int b4 = (buf[0] << 24) + ... + (buf[3]);
    unsigned int b2 = (buf[0] << 8) + (buf[1]);
    

    Shifting involves type promotions, and indefinite sign extensions (given the implementation-defined nature of char). Basically you always want everything to be unsigned when manipulating bits.

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

Sidebar

Related Questions

We have a binary file from which we have identified the following dates (as
I have one binary file which I have created. In it, data is stored
I have a binary file to which I want to append a chunk of
I have a binary file to which I'm trying to write however I dont
Say I have a binary file (generated with Java) containing a 32 bit int
I have binary data in a file that I can read into a byte
I am parsing a binary file using a specification. The file comes in big-endian
I have a problem with binary type. I have binary file with data. Every
I have a binary file that was created on a unix machine. It's just
I have a binary file which I am reading to a collection of byte

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.