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!
Not quite.
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.
to read the struct you will need to reverse:
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.hare 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 bytechars, 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::reversefunction: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.