By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

Converting Little Endian to big endian for a union

P: n/a
Hi,
I am sending this question again as new question rather than reply to old
question
Please refer below:
struct raw_data
{
unsigned char x;
unsigned char y;
};
union full_data
{
unsigned int actual_value; //int size is 2 bytes
struct raw_data A; //2 chars means 2 bytes
};

I have two compilers for two different controllers, one with little endian
and one with big endian support.
The number "actual_value" is different in both cases because of different
endian-ness.
The actual raw_data structure I use is bigger than this example(8 bytes) and
the data is
interpreted in a structure having 4 integers inside the union.
I would like to know fastest way to do little to big ENDIAN conversion or
vice versa.
I presently interchange two successive bytes (x and y here in example)
before
accessing actual_value for porting purpose.
Last but not the least, I am porting from C16X controller to X51 and using
Keil compiler
for both the versions.

Thank you for the help.
Regards,
Shailendra
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mehta Shailendrakumar wrote:

I am sending this question again as new question rather than
reply to old question .... snip ...
I have two compilers for two different controllers, one with
little endian and one with big endian support.


You are asking the wrong question (I think). I believe you have a
defined data stream, with a defined byte order for 16 bit numbers.
That's all you need to know to create portable code. Lets assume
the transmitted numbers appear ms byte first (which is the normal
network order) and that those network bytes are 8 bits (which the
machine bytes need not be). Then we can write:

unsigned int net2uint(FILE *fp)
{
unsigned int tmp;
int ch;

/* ignoring the possibility of EOF for now */
ch = getc(fp);
tmp = ch & 0xff;
ch = getc(fp) & 0xff;
return 256 * tmp + ch;
}

which doesn't care a hoot about the byte sex of the host. If
things aren't coming from a file make suitable adjustments. You
can write a uint2net in a similar manner, using only arithmetic,
and again independent of endianess.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #2

P: n/a
Me
<snip about 16 bit unsigned ints>
I have two compilers for two different controllers, one with little endian
and one with big endian support.
The number "actual_value" is different in both cases because of different
endian-ness. <snip> Last but not the least, I am porting from C16X controller to X51 and using
Keil compiler
for both the versions.


I googled your compiler's manual and you can use the rotate intrinsic
functions to do this: _ror(actual_value, 8). This may (or may not) be
the fastest way to do this, but this newsgroup is definitely not the
right place to find out that kind of information.

Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.