473,480 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

byte order problem

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
Jul 22 '05 #1
4 2464
> 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
Jul 22 '05 #2

"Niels Dybdahl" <nd*@fjern.detteesko-graphics.com> schrieb im
Newsbeitrag news:40*********************@dtext02.news.tele.dk. ..
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,


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


Jul 22 '05 #3
Gernot Frisch wrote:
<snip>
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.
- Type casts to access parts of variables,

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.

<snip>
- Bit operations; for example for graphics.

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.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #4

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
Jul 22 '05 #5

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

Similar topics

13
7864
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
2
2899
by: Donal McWeeney | last post by:
Hi, I think I may have a slight encoding problem with what I am doing. I have a test console app that takes an xml document and splits it into a number of smaller xml documents. First thing...
2
4517
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
14
1929
by: gamja | last post by:
Hi all. This is my first post on this group. Nice to meet you, cool guys~! I'm on system programming on various embedded systems and understand very well the byte alignment issues. When I write...
12
7297
by: Michi Henning | last post by:
Looking at the language spec, I can't find a statement about the byte order for value types, such as int, float, etc. Are they guaranteed to be little-endian or big- endian? I know that, on a...
6
10124
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
6
2175
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char....
4
2201
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
5
3447
by: moni | last post by:
Hey, My buffer contains a short int, some char, and a structure in form of a byte array. Read the string as: TextBox4.Text = System.Text.Encoding.ASCII.GetString(buffer1, 0, 31); Read...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6737
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...
0
5336
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,...
0
4481
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...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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 ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
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...

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.