Converting Little Endian to big endian for a union

  • Thread starter Mehta Shailendrakumar
  • Start date
M

Mehta Shailendrakumar

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
 
C

CBFalconer

Mehta said:
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.
 
M

Me

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.
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top