byte order problem

G

Gernot Frisch

Hi,

when porint apps to powerpc the byte order get's swapped to
low-endian. Now, what problems can occur and what should I take care
of when porting to mac e.g.?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
N

Niels Dybdahl

when porint apps to powerpc the byte order get's swapped to
low-endian. Now, what problems can occur and what should I take care
of when porting to mac e.g.?

Anything like:
- Reading binary files,
- Type casts to access parts of variables,
- Reading of unicode packed in UTF16 or similar,
- Bit operations; for example for graphics.

Niels Dybdahl
 
G

Gernot Frisch

Niels Dybdahl said:
Anything like:
- Reading binary files,

How about:
struct
{
long a;
long b;
}s;

fread(&s, sizeof(s), file); ??

- Type casts to access parts of variables,

unsigned long a = 0xFFFF0000;
unsigned char b = (unsigned char) a;
Does this cause a problem?

- Reading of unicode packed in UTF16 or similar,

Oh, OK. I use 8bit characters, but thank you.
- Bit operations; for example for graphics.

long a = 0xffff;
long b = a& 0xff ??

How do developers handle these problems?
-Gernot
 
S

Stewart Gordon

Gernot Frisch wrote:
How about:
struct
{
long a;
long b;
}s;

fread(&s, sizeof(s), file); ??

That'll indeed read each long according to the platform byte-order. See
below.
unsigned long a = 0xFFFF0000;
unsigned char b = (unsigned char) a;
Does this cause a problem?

No. Casts between integer types always work from the little end,
regardless of byte order.

long a = 0xffff;
long b = a& 0xff ??

No problem there. Hexadecimal literals are specified in standard
hexadecimal notation.
How do developers handle these problems?
-Gernot

The only problem you've addressed is reading data from a binary file.
Assuming that you want to be able to share data files between platforms,
you need to decide on a byte order as part of the file format. Then use
some functions to convert endianness as needed.

Here's a bit of some code I used for this:

----------
union ByteOrderer {
char b[4];
short s;
long l;
};

short LEShort(short s) {
union ByteOrderer le;

le.b[0] = (char) (s & 0xFF);
le.b[1] = (char) ((s & 0xFF00) >> 8);

return le.s;
}
----------

LEShort converts a short from little-endian to native BO or vice versa.
It's trivial to write LELong, BEShort and BELong on the same basis.
Of course, chances are you won't need both LE and BE functions in the
same program, unless you're reading/writing a variety of formats that
use different orders.

The other kind of problem you're likely to encounter is if you try to
access the same contents of memory as different types, either by casting
a pointer or using a union. (OK, so my snippet does this, but that's
because the whole point is to convert endianness.) If you want
portability here, you'll probably need to rewrite the code e.g. to use
shifts instead.

Stewart.
 
G

Gernot Frisch

The other kind of problem you're likely to encounter is if you try to
access the same contents of memory as different types, either by casting
a pointer or using a union. (OK, so my snippet does this, but that's
because the whole point is to convert endianness.) If you want
portability here, you'll probably need to rewrite the code e.g. to use
shifts instead.


Ah, OK. So if I'll use long as long and pointer to something as
pointer to something and not as pointer to somethins else, everything
is likely to work good. How about std c++ library's fstream clasS?
Does it already take care of this?
But it's not really a problem since I'm using my own
"WriteToFile_long(long data)", since I already expected this kind of
problems.

-Gernot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

unreachable code? 4
int(a) vs (int)a 19
reference to parent inherritance? 9
queue where I can delete in the middle? 3
What's a smart pointer? 7
boost 2
a[3} slower than a.x; a.z; a.z 27
a thread safe stack? 7

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top