473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

iostream hierarchy to support endianess

Hello,

I am working on a project where I need to serialize my objects to a
stream in a mixed ASCII/binary way. Using the c++ iostream was
extremely straighforward and I quickly made progress. Unfortunately I
need to handle endianess (reading big endian binary file on little
endian architecture and vice versa). In order to do that I copy paste a
hierachy-like structure for my own IOStream. Basically I have (*)
All I need to do then is to redefine the binary stream operation
(istream::read, ostream::write) to byteswap the bytes array depending
on the SwapCode value. The formatted operations remain exactly the
same. Not supporting endianness is not an option for me (this is in the
file format description).

Is there another (easier?) way to achieve this ?

Thanks for your comments,
Mathieu

(*)
class IOSBase {};
class IOS : public IOSBase
{
public:
IOS () {}
~IOS() {}
SwapCode const &GetSwapCode() const { return m_SwapCode; }
protected:
SwapCode m_SwapCode;
};
class IStream : virtual public IOS
{
....// skipped to avoid too much details
};
class OStream : virtual public IOS
{
....// skipped to avoid too much details
};
class IOStream : public IStream, public OStream{};

And then I can simply do:
class StringStream : public IOStream
{
std::stringstream Internal;
};
or
class IFStream : public IStream
{
std::ifstream Internal;
};

Jun 6 '06 #1
1 3135

mathieu wrote:
Hello,

I am working on a project where I need to serialize my objects to a
stream in a mixed ASCII/binary way. Using the c++ iostream was
extremely straighforward and I quickly made progress. Unfortunately I
need to handle endianess (reading big endian binary file on little
endian architecture and vice versa). In order to do that I copy paste a
hierachy-like structure for my own IOStream. Basically I have (*)
All I need to do then is to redefine the binary stream operation
(istream::read, ostream::write) to byteswap the bytes array depending
on the SwapCode value. The formatted operations remain exactly the
same. Not supporting endianness is not an option for me (this is in the
file format description).

Is there another (easier?) way to achieve this ?

Thanks for your comments,
Mathieu


I did not have the right keyword to search into this very same
newsgroup. Instead searching for "binary streams" lead to exactly what
I wanted. Most refs were sent by Dietmar Kuehl

http://groups.google.com/group/comp....8518518f57dda1

and his binio is still available at:

http://www.dietmar-kuehl.de/cxxrt/

Mathieu

Jun 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sigmund Skjelnes | last post by:
Hi! Trying to get a simple cpp program to compile, I'd got the linker complaining it could'nt find eiter cout nor endl or other object which belong to iostream. I'd had to use stdiolib to get the...
5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
4
by: Gernot Frisch | last post by:
Hi, does the ARM processors have different endians than x86 platform? For ints and/or floating points??? I get a really strange objec when loading it from a file. Creating manually works. ...
8
by: | last post by:
Well! Maybe I wrote this word incorrect but its not included im my e-dictionary! I have a structure with many int, short etc I execute this function: fread(&my_struct, sizeof(struct), 1,...
12
by: Oliver Knoll | last post by:
Ok, I've searched this group for Big/Little endian issues, don't kill me, I know endianess issues have been discussed a 1000 times. But my question is a bit different: I've seen the follwing...
1
by: Gustaf Liljegren | last post by:
My input data consists of a list of parent-child relationships. One item in this list can be modelled like this: +---------+ | Item | +---------+ | Parent | | Child | +---------+
1
by: Bill Sun | last post by:
Hi, I just want to using a console application in the .net enviroment. Before the main(), I using the #include "iostream.h" but the .net tell me:...
8
by: mmu2643 | last post by:
Hi, I was trying to compile the following bit of code: #include <iostream.h> main(){ cout<<"Hello World!";
2
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.