Archived

This topic is now archived and is closed to further replies.

taliesin73

A question of design

Recommended Posts

I am currently looking at coding up some routines to convert little-endian to big endian. What I am trying to determine is the best place to do the conversion. One possibility I thought of is a wrapper class around each int, which works, but it also means more mucking about with typecasting in the event of needing to write the data out big endian (or indeed reading it in little endian) - and it''s necessary to know at all times the data you are dealing with. A better solution would seem to be using a decorator on the stream, to keep all objects unaware of what is coming in - but the standard C++ stream objects don''t have methods for reading in ints (or do they?), so that means the decorator can''t be substituted without writing my own stream hierarchy. Currently I am just using a couple of free functions, so I can feel like I''m making progress. What would everybody recommend in this circumstance? --

Get a stripper on your desktop!

Share this post


Link to post
Share on other sites
Not sure if its any use but found this on my HD...

#ifdef _SGI_SOURCE
#define __BIG_ENDIAN__
#endif

#ifdef __BIG_ENDIAN__

short LittleShort (short l)
{
byte b1,b2;

b1 = l&255;
b2 = (l>>8)&255;

return (b1<<8) + b2;
}

short BigShort (short l)
{
return l;
}


int LittleLong (int l)
{
byte b1,b2,b3,b4;

b1 = l&255;
b2 = (l>>8)&255;
b3 = (l>>16)&255;
b4 = (l>>24)&255;

return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
}

int BigLong (int l)
{
return l;
}


float LittleFloat (float l)
{
union {byte b[4]; float f;} in, out;

in.f = l;
out.b[0] = in.b[3];
out.b[1] = in.b[2];
out.b[2] = in.b[1];
out.b[3] = in.b[0];

return out.f;
}

float BigFloat (float l)
{
return l;
}


#else


short BigShort (short l)
{
byte b1,b2;

b1 = l&255;
b2 = (l>>8)&255;

return (b1<<8) + b2;
}

short LittleShort (short l)
{
return l;
}


int BigLong (int l)
{
byte b1,b2,b3,b4;

b1 = l&255;
b2 = (l>>8)&255;
b3 = (l>>16)&255;
b4 = (l>>24)&255;

return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
}

int LittleLong (int l)
{
return l;
}

float BigFloat (float l)
{
union {byte b[4]; float f;} in, out;

in.f = l;
out.b[0] = in.b[3];
out.b[1] = in.b[2];
out.b[2] = in.b[1];
out.b[3] = in.b[0];

return out.f;
}

float LittleFloat (float l)
{
return l;
}


#endif

Share this post


Link to post
Share on other sites
Don''t know if this is helpful but my suggestion is to use wrapper classes, for example Integer for ints.

Then, overload the << and >> operators so that they behave correctly on their respective machines.

Anyway, that''s what I''d do. Porbably ain''t the best idea.

Share this post


Link to post
Share on other sites