473,799 Members | 3,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Endianess: why does this code not change value on BE machine?

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):

/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I'm really curious...

And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")? :) Hope I can answer the 2nd quesion
myself when I understand the 1st question... :)
Here's the complete test program:

#include <stdlib.h>
#include <stdio.h>

/* This function is useful when reading and converting data which is
stored
in Big Endian Format.
- this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

int main (int argc, char **argv)
{
int a;
char c;
short data1;
short data2;
char *d;

(void)argc;
(void)argv;

a = 0x01020304;
c = ((char *)&a)[0];

if (c == 1)
{
fprintf (stdout, "Integer a: %x - first byte: %x (MSB) -> Big
endian machine.\n", a, c);
}
else if (c == 4)
{
fprintf (stdout, "Integer a: %x - first byte: %x (LSB) -> Little
endian machine.\n", a, c);
}
else
{
fprintf (stdout, "Integer a: %x - first byte: %x -> A weirdo
machine.\n", a, c);
}

data1 = 0x0102;
d = ((char *)&data1);

fprintf (stdout, "Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));

data2 = getShortBE ((char *)&data1);

d = ((char *)&data2);
fprintf (stdout, "After GET: Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));

return 1;
}

Output:
-------

On Sun:
./Endian

Integer a: 1020304 - first byte: 1 (MSB) -> Big endian machine.
Data[0]: 1 - Data[1]: 2
After GET: Data[0]: 1 - Data[1]: 2

On Linux (i386):

Integer a: 1020304 - first byte: 4 (LSB) -> Little endian machine.
Data[0]: 2 - Data[1]: 1
After GET: Data[0]: 1 - Data[1]: 2

Thanks, Oliver
Nov 14 '05 #1
12 1648

"Oliver Knoll" <tk****@bluewin .ch> wrote

/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I'm really curious...
Firstly arbitrary data should be unsigned char. Plain char is for actual
text.

Don't be fooled by the << operator. This suggests that data is being shifted
"leftwards" in memory, but in fact it always moves less significant bits to
the more significant position.
Your function therefore takes an arbitrary stream of bytes, and treats the
first one as the top eight bits and the second one as the lower eight bits
of a 16-bit number.
Incidentally it will not work as expected if CHAR_BIT is not eight, which it
isn not always. short isn't necessarily sixteen bits, either.
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")? :) Hope I can answer the 2nd quesion
myself when I understand the 1st question... :)
So you can treat your arbitrary bit stream as little endian simply by
placing the first byte in the least-significant position, and shifting up
the second byte to the most significant position.

Nov 14 '05 #2
In article <b2************ **************@ posting.google. com>,
Oliver Knoll <tk****@bluewin .ch> wrote:
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I'm really curious...


Well, one way to see it is that this function doesn't depend on
endianness at all, so it will give the same result on a big- or
little-endian machine. But just accesing the data as a short will
give different results on the two.

-- Richard
Nov 14 '05 #3
tk****@bluewin. ch (Oliver Knoll) wrote:
/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ^^^^^^ useless cast ((data[0] << 8) | data[1]);
Implementation-defined behaviour, due to sign-extension. You should
use unsigned data types for bit manipuation, eg:

short getShortBE(void *data)
{
unsigned char *p = data;
return ( p[0] << CHAR_BIT ) | p[1] ; } I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine?
Explain in your own words why it works on an LE machine, and
then it should be obvious why it does nothing on BE.
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")?


Switch the '0' with the '1'.
Nov 14 '05 #4
On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:
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,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):
it is wrong because if a char "x" is 8 bit then x<<8 == 0
/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

#include <limits.h>

/* Suppone 2*sizeof(char)= sizeof(short) */
unsigned short GetShort(unsign ed char *data)
{unsigned short u=data[0];
return (u<<CHAR_BIT)| data[1];
}
Nov 14 '05 #5

On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,


where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation; that's bad.

-Arthur

Nov 14 '05 #6
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message news:<ci******* ***@news5.svr.p ol.co.uk>...
"Oliver Knoll" <tk****@bluewin .ch> wrote

/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
,,,
Firstly arbitrary data should be unsigned char. Plain char is for actual
text.


Thanks :) Good point. (In the real world I'm using such ugly stuff
like Q_UINT16 and the like though - I guess I've never seen a library
(Qt in this case) which doesn't define their own "datatypes" to ensure
correct byte-siyes ;)
Don't be fooled by the << operator. This suggests that data is being shifted
"leftwards" in memory, but in fact it always moves less significant bits to
the more significant position.
Ahh, that's exactly the explanation I was looking for, my brain was
stuck with this "bits go to the left". It makes perfectly sense now,
thanks a lot!
...
Incidentally it will not work as expected if CHAR_BIT is not eight, which it
isn not always. short isn't necessarily sixteen bits, either.


I've taken char and short and was naively assuming them to be 8 and 16
bit for illustration purposes.

Thanks, Oliver
Nov 14 '05 #7
On Mon, 13 Sep 2004 03:13:24 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote:
On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation ; that's bad.

static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}


where is the "arithmetic promotion"? I don't know how it can be ok
data[0] is a char and do << 8 (it seems an error to me)
then or data[1]; then the promotion to short
[if it was return (short) (data[0]<<8) | data[1]; you are right]
but (short) ((data[0]<<8) | data[1])
seems to me if *data is 8 bit = (short) (data[1])
-Arthur


Nov 14 '05 #8
On Mon, 13 Sep 2004 03:13:24 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote:
On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation ; that's bad.

static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}


where is the "arithmetic promotion"? I don't know how it can be ok
data[0] is a char and do << 8 (it seems an error to me)
then or data[1]; then the promotion to short
[if it was return (short) (data[0]<<8) | data[1]; you are right]
but (short) ((data[0]<<8) | data[1])
seems to me if *data is 8 bit = (short) (data[1])
-Arthur


Nov 14 '05 #9
On Mon, 13 Sep 2004 03:13:24 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote:
On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation ; that's bad.

static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}


where is the "arithmetic promotion"? I don't know how it can be ok
data[0] is a char and do << 8 (it seems an error to me)
then or data[1]; then the promotion to short
[if it was return (short) (data[0]<<8) | data[1]; you are right]
but (short) ((data[0]<<8) | data[1])
seems to me if *data is 8 bit = (short) (data[1])
-Arthur


Nov 14 '05 #10

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

Similar topics

7
4862
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As InternetExplorer But then I cant see when a user exit my objIExplorer and than an error will show up when I try to open a link in the IE window that does not exist... What can I do about this and way does it not work in win 98?
4
1963
by: pellepluttrick | last post by:
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()
5
5506
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
4087
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.
35
5482
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : %d\n",sizeof(unsigned char)); printf("short : %d\n",sizeof(short)); printf("unsigned short : %d\n",sizeof(unsigned short)); printf("int : %d\n",sizeof(int));
1
3158
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. 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...
2
2358
by: dougloj | last post by:
Hi. I have an ASP.NET application written in C#. To log in, a user must provide their email address and password. I already give the user a "Remember my Email Address" check box. If they check it when logging in, I store the email address in a cookie and automatically display the address when they login again. I now want to give the user a "Remember my Password" checkbox. If they check this new checkbox, I'm planning on encrypting...
2
5017
by: StanB | last post by:
I came across this weird problem: 1. Session state stops working after the app is deployed to another server because IE does not accept cookies. 2. It works if cookieless="true" in the web.config 3. Yes, I tried IE - Tools - Privacy - Accept All Cookies and also Override automatic cookie handling, Always allow session cookes
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10228
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
9072
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
7565
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
6805
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
5463
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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
3
2938
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.