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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:05:56+00:00 2026-05-15T11:05:56+00:00

I am parsing a binary file using a specification. The file comes in big-endian

  • 0

I am parsing a binary file using a specification. The file comes in big-endian mode because it has streamed packets accumulated. I have to reverse the length of the packets in order to “reinterpret_cast” them into the right variable type. (I am not able to use net/inet.h function because the packets has different lengths).

The read() method of the ifstream class puts the bytes inside an array of chart pointers. I tried to do the reversion by hand using a but I cannot figure out how to pass the “list of pointers” in order to change their position in the array.

If someone knows a more efficent way to do so, please let me know (8gb of data needs to be parse).

#include <iostream>
#include <fstream>

void reverse(char &array[]);

using namespace std;

int main ()
{
    char *a[5];
    *a[0]='a'; *a[1]='b'; *a[2]='c'; *a[3]='d'; *a[4]='e';

    reverse(a);

    int i=0;
    while(i<=4)
    {
        cout << *a[i] << endl;
        i++;
    }
    return 0;
}
void reverse(char &array[])
{
    int size = sizeof(array[])+1;
    //int size = 5;
    cout << "ARRAY SIZE: " << size << endl;

    char aux;
    for (int i=0;i<size/2;i++)
    {
            aux=array[i];
            array[i]=array[size-i-1];
            array[size-i-1]=aux;
    }
}

Thanks all of you for your 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-15T11:05:56+00:00Added an answer on May 15, 2026 at 11:05 am

    Not quite.

    The file comes in big-endian mode because it has streamed packets accumulated. I have to reverse the length of the packets in order to “reinterpret_cast” them into the right variable type.

    You need to reverse the bytes on the level of stored data, not the file and not the packets.

    For example, if a file stores a struct.

    struct S {
      int i;
      double d;
      char c;
    };
    

    to read the struct you will need to reverse:

    int: [4321]->[1234]  // sizeof(int) == 4, swap the order of 4 bytes
    double: [87654321]->[12345678]  // sizeof(double) == 8, swap the order of 8 bytes
    char: [1]->[1]  // sizeof(char) == 1, swap 1 byte (no swapping needed)
    

    Not the entire struct at once.

    Unfortunately, it’s not as trivial as just reversing the block of data in the file, or the file itself. You need to know exactly what data type is being stored, and reverse the bytes in it.

    The functions in inet.h are used for exactly this purpose, so I encourage you to use them.

    So, that brings us to c strings. If you’re storing c strings in a file, do you need to swap their endianness? Well, a c string is a sequence of 1 byte chars. You don’t need to swap 1 byte chars, so you don’t need to swap the data in a c string!

    If you really want to swap 6 bytes, you can use the std::reverse function:

    char in[6] = get6bytes();
    cout << in << endl;  // shows abcdef 
    std::reverse(in, in+6);
    cout << in << endl;  // shows fedcba
    

    If you’re doing this on any large scale (a large amount of types), then you may want to consider writing a code generator that generates these byte swapping functions (and file reading functions), it’s not too hard, as long as you can find a tool to parse the structs in c (I’ve used gcc-xml for this, or maybe clang would help).

    This makes serialization a harder problem. If it’s in your power, you may want to consider using XML or Google’s protocol buffers to solve these problems for you.

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

Sidebar

Related Questions

I'm parsing a binary file format. It encodes an integer using four bytes in
I have a binary file, inside of which has multiple frames. Each frame starts
I am reading a binary file into a parsing program. I will need to
Does anybody recommend a design pattern for taking a binary data file, parsing parts
I am parsing binary file. File size can be large. I want to search
I am parsing binary files and have to implement a CRC algorithm to ensure
Say I have a binary file (generated with Java) containing a 32 bit int
I'm parsing a binary file in javascript that is storing two pieces of information
Parsing a text file in vb.net and need to locate the latitude and longitude
I'm parsing text from a file and storing it in a string. The problem

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.