473,809 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::stringstre am Internal;
};
or
class IFStream : public IStream
{
std::ifstream Internal;
};

Jun 6 '06 #1
1 3158

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
2213
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 program working. I'd have attached the source code, some of it is Norwegian, but I'd think it will make sense anyway. Do anybody have a clue why the library is missing? The code compiled ok because the .h file is there, but the linker is...
5
5511
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(): "...each character is 1 if the corresponding bit is set, and 0 if it is not. In general, character position i corresponds to bit position N - 1 -
4
3477
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. -- -Gernot
8
4089
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, filepointer); The problem is this: In different endianess platforms from same file will be loaded different data in the class.
12
1651
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 function several times, it converts data stored in Big Endian (BE) format into host native format (LE on LE machines, BE on BE machines):
1
3134
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
4536
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: c:\project\console\console\console.cpp(11): fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
8
2737
by: mmu2643 | last post by:
Hi, I was trying to compile the following bit of code: #include <iostream.h> main(){ cout<<"Hello World!";
2
2016
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 traversing on specific data structures. For each iterator I want to be able to specify the four possible const combinations, corresponding to the various void* for pointers, with the possibility to perform conversions from non-const to const just like...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.