473,219 Members | 1,635 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,219 software developers and data experts.

Endianess...

Hi,
I thought I understood this stuff - but...

This little program (taken from W. Richard Stevens Unix Network
Programming Volume 1) determines a machine's endianess:

#include <iostream>
using namespace std;

int main()
{
union {
unsigned short us;
char c[sizeof(unsigned short)];
} un;

un.us = 0x1234;

if (sizeof(unsigned short) == 2) {
if (un.c[0] == 0x12 && un.c[1] == 0x34)
cout << "big-endian\n";
else if (un.c[0] == 0x34 && un.c[1] == 0x12)
cout << "little-endian\n";
else
cout << "unknown\n";
} else
cout << "sizeof(unsigned short) = " << sizeof(unsigned short)
<< ".\n";
}

On my machine it prints little-endian (as expected). This obviously
must mean that the bits are laid out in memory as: "0x34, 0x12". Right?

So if I want to send an unsigned short over the network from this
machine (which should be done in network byte-order/big-endian) I must
convert the order of the bytes in memory so this unsigned short is sent
as "0x12, 0x34". Right? (Oh, btw I do not have htons on this platform
:-( ... )

Now the following short block of code converts the unsigned short into
a stream of two bytes:

// Remember: stored in memory as 0x34 0x12
unsigned short us = 0x1234;

char buf[2];

// Convert from little-endian to big endian
buf[0] = us & 0xFF; // Should now contain 0x12
buf[1] = (us >> 8) & 0xFF; // Should now contain 0x34

// Print the stream...
printf("0x%.2X", buf[0]);
printf(", ");
printf("0x%.2X", buf[1]);
printf("\n");

To my great surprise it printed:
0x34, 0x12

What?!?!?!?

If I change to:
buf[0] = (us >> 8) & 0xFF;
buf[1] = us & 0xFF;
everything works OK but I think it should not! Sigh...
Please enlight me!

/Pelle

Jul 23 '05 #1
4 1939
pe************@yahoo.com wrote:
[...]
// Remember: stored in memory as 0x34 0x12
That means that 0x34 has *lower* address than 0x12.
unsigned short us = 0x1234;

char buf[2];

// Convert from little-endian to big endian
buf[0] = us & 0xFF; // Should now contain 0x12
Why? (0x1234 & 0x00ff) gives 0x34.
buf[1] = (us >> 8) & 0xFF; // Should now contain 0x34
Why? (0x1234 >> 8) gives 0x12.
// Print the stream...
printf("0x%.2X", buf[0]);
printf(", ");
printf("0x%.2X", buf[1]);
printf("\n");

To my great surprise it printed:
0x34, 0x12

What?!?!?!?

If I change to:
buf[0] = (us >> 8) & 0xFF;
buf[1] = us & 0xFF;
everything works OK but I think it should not! Sigh...
Please enlight me!


Write down what the bit patterns look like and perform shifts and ANDs.

Victor
Jul 23 '05 #2
Victor Bazarov wrote:
pe************@yahoo.com wrote:
[...]
// Remember: stored in memory as 0x34 0x12

That means that 0x34 has *lower* address than 0x12.
unsigned short us = 0x1234;

char buf[2];

// Convert from little-endian to big endian
buf[0] = us & 0xFF; // Should now contain 0x12

Why? (0x1234 & 0x00ff) gives 0x34.


Wanted to add: "...regardless of endianness."
buf[1] = (us >> 8) & 0xFF; // Should now contain 0x34

Why? (0x1234 >> 8) gives 0x12.


regardless of endianness, again.
[...]

Jul 23 '05 #3
pe************@yahoo.com wrote:
Hi,
I thought I understood this stuff - but...


I'm not sure what you want but this is a post in an earlier thread that
might give you some ideas.

http://groups-beta.google.com/group/...a?dmode=source
Jul 23 '05 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:gs******************@newsread1.mlpsca01.us.to .verio.net...
Victor Bazarov wrote:
pe************@yahoo.com wrote:
[...]
// Remember: stored in memory as 0x34 0x12

That means that 0x34 has *lower* address than 0x12.
unsigned short us = 0x1234;

char buf[2];

// Convert from little-endian to big endian
buf[0] = us & 0xFF; // Should now contain 0x12

Why? (0x1234 & 0x00ff) gives 0x34.


Wanted to add: "...regardless of endianness."
buf[1] = (us >> 8) & 0xFF; // Should now contain 0x34

Why? (0x1234 >> 8) gives 0x12.


regardless of endianness, again.
[...]


Hi Pelle

Just in case it wasn't clear to you what Victor was saying: when you
shift bits like that, you don't have to worry about what the order of bytes
in the physical memory is. Just treat it like you would on paper. Shifting
to the right, for example, will shift the high-order bits towards the
low-order side, and it does not matter whether the low-order byte is stored
in a higher or lower location in physical memory. So, using your example,
doing 0x1234 >> 8 will always result in 0x0012, and never in 0x3400,
reagardless of the machine's byte ordering.

You only really need to worry about byte ordering when reading values
from a data stream on a machine that has one ordering when that data was
written using the opposite ordering.

(What our software does is use a compile flag that, when compiling for
the PC, byte ordering is intentionally reversed when reading or writing, so
that we know it will match what's done on the Mac. When compiled on the
Mac, reading and writing is done without changing the ordering. We use
#defines and #ifdefs to determine whether to call the order-reversing code
or not.)

-Howard

Jul 23 '05 #5

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

Similar topics

6
by: hantheman | last post by:
Is this a portable implementation? #if defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN) #define htons(A) (A) #define htonl(A) (A) #define ntohs(A) (A) #define ntohl(A) (A) #elif...
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: mathieu | last post by:
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....
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.