Converting Little Endian to big endian

I

invincible

hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))

But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2

Thanks for help
 
O

Old Wolf

invincible said:
hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))

I think you mean:
bValue = (aValue said:
But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2

This method only works if aValue and bValue are unsigned,
and bValue is a 16-bit value.

Try it again with them being 'unsigned short' and see if it works.

Or you could modify the shift:

bValue = (((unsigned short)aValue) << 8)
| (((unsigned short)aValue) >> 8);

Of course, this will leave garbage in the higher bits of bValue
if bValue is more than 16 bits.
 
L

Lawrence Kirby

hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

Firstly make sure that you use unsigned types for bit manipulation liek
this, the rules for signed types don't guarantee the results you expect or
even that the operation is well defined.
This can be done

bValue = ( (aValue << 8) | (aValue << 8))

I take it you mean

bValue = ( (aValue << 8) | (aValue >> 8));

Don't assume the type you are using is exactly 16 bits, C generally
specifies MINIMUM ranges for types, they can be wider. So

bValue = ( ((aValue & 0xff) << 8) | (aValue >> 8));

or belts and braces:

bValue = ( ((aValue & 0xff) << 8) | ((aValue >> 8) & 0xff));

Note also that C doesn't require a byte to be 8 bits, it can have more.
Maybe what you are doing only makes sense if it is, but if you wanted to
use the more general C definition of a byte you could use for example

#include <limits.h>

bValue = ( ((aValue & UCHAR_MAX) << CHAR_BIT) |
((aValue >> CHAR_BIT) & UCHAR_MAX) );
But how about if value is bValue = F26B I am getting FFF2 instead of
6BF2

You were probably using a signed integer type. A common implementation
for right shifting signed values is to use "sign extension", i.e. instead
of shifting zeroes in from the left the sign bit is duplicated.

Lawrence
 
J

Joe Wright

invincible said:
hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))

But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2

Thanks for help

This is the Swap approach. Enjoy.

void swends(void *v, size_t width) {
int x;
uchar *l = v, *r = l + (width - 1);
while (l < r)
x = *l, *l++ = *r, *r-- = x;
}
 
M

Mehta Shailendrakumar

Hi,

I have a different question in same topic:

Please refer below:

struct raw_data
{
unsigned char x;
unsigned char y;
};

union full_data
{
unsigned int actual_value; //2 bytes
struct raw_data A; //2 bytes
};


I have two compilers for two different controllers, one with little endian
and one with big endian support.
The actual structure I use is bigger than this (8 bytes) and the data is
interpreted in 4 integers inside the union.

I would like to know fastest way to do such ENDIAN conversion.
I presently exchange two successive bytes (x and y here in example) before
accession actual_value for porting purpose.
Thank you for the help.

Regards,
Shailendra
 
K

Keith Thompson

Mehta Shailendrakumar said:
I have a different question in same topic:

Please refer below:

struct raw_data
{
unsigned char x;
unsigned char y;
};

union full_data
{
unsigned int actual_value; //2 bytes
struct raw_data A; //2 bytes
};


I have two compilers for two different controllers, one with little endian
and one with big endian support.
The actual structure I use is bigger than this (8 bytes) and the data is
interpreted in 4 integers inside the union.

I would like to know fastest way to do such ENDIAN conversion.
I presently exchange two successive bytes (x and y here in example) before
accession actual_value for porting purpose.

Please don't top-post. Your response belongs after any quoted text.

Why do you care about the fastest way to do it? If you write your
code as clearly as possible, the compiler is likely to generate code
that's fast enough for your purposes.

Given two different methods for swapping bytes, there's generally no
way we can tell you which one is going to be faster. It depends on
the machine, on the compiler, on how the compiler is invoked, and for
all we know on the phase of the moon. If your code turns out not to
be fast enough, you can try different methods and measure their speed.

BTW, your code above assumes that an int is two bytes; that's a
non-portable assumption.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top