On Tue, 14 Jun 2005 09:46:35 +0530, "invincible"
First, if you are going to post the same question in multiple
newsgroups, you should cross-post it, not post it separately to each
group.
hi I wanted to convert Little Endian to big endian for 2 byte value
Two bytes equals 32 bits on one platform I work on, and 64 bits on
another that I haven't used for a few years. Do you mean a 16-bit
value?
for example
if hex value of aValue = 0102
required valu e = 0201
This can be done
Yes, it can.
bValue = ( (aValue << 8) | (aValue << 8))
But not like that!
But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2
Thanks for help
Bit-shift and logical operators should not be used on signed integer
types unless you know exactly what you are doing, and even then only
rarely.
Used unsigned types.
#include <stdio.h>
unsigned int swap_octets(unsigned int aval)
{
return ((aval << 8) | (aval >> 8)) & 0xffff;
}
int main()
{
unsigned short aval;
unsigned short bval;
aval = 0x0201;
bval = (aval << 8) | (aval << 8);
printf("Your method, %04X produces %04X\n", aval, bval);
printf("%04X reverses to %04X\n", aval, swap_octets(aval));
aval = 0xF26B;
printf("%04X reverses to %04X\n", aval, swap_octets(aval));
return 0;
}