# Little Endian to Big Endian

Discussion in 'C++' started by invincible, Jun 14, 2005.

1. ### invincibleGuest

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

invincible, Jun 14, 2005

2. ### Jack KleinGuest

On Tue, 14 Jun 2005 09:46:35 +0530, "invincible"
<> wrote in comp.lang.c++:

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;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jack Klein, Jun 14, 2005

3. ### Gianni MarianiGuest

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

If aValue is signed, you need to remove the sign extension.

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

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

Gianni Mariani, Jun 14, 2005
4. ### Robbie HatleyGuest

"tuvok" <> wrote in message
news:d8lp4h\$aoc\$03\$-online.com...
> "invincible" wrote
> > 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

>
> Try this:
> bValue = (aValue << 8) | (aValue >> 8);

Hmmm... I think that would work if you make aValue and
bValue unsigned. But if they're signed, that right shift
is going to pour-in a bunch of 1s if the msb of aValue
is a 1.

example:

#include <iostream>
int main(void)
{
register signed int Blat = 0x80000000;
Blat = Blat >> 21;
std::cout << std::hex << Blat << std::endl;
return 0;
}

Do you expect that to print 400?
No, it prints fffffc00.

BUT, if you make it unsigned:

#include <iostream>
int main(void)
{
register unsigned int Blat = 0x80000000;
Blat = Blat >> 21;
std::cout << std::hex << Blat << std::endl;
return 0;
}

then it prints 400 as expected.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Robbie Hatley, Jun 14, 2005
5. ### Robbie HatleyGuest

"invincible" <> wrote in message
news:d8lln6\$46b\$...
> 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

You're sign-extending. You must be using a C++ implimentation
which uses 16-bit int. (Or you're using short int.)

Use unsigned ints to avoid that problem:

#include <iostream>
int main(void)
{
unsigned int aValue, bValue;
aValue = 0xA857;
bValue = ((aValue&0x00FF)<<8) | ((aValue&0xFF00)>>8);
std::cout << std::hex << bValue << std::endl;
return 0;
}

Prints "57A8".

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Robbie Hatley, Jun 14, 2005
6. ### Dana CartwrightGuest

"Gianni Mariani" <> wrote in message
news:...
> invincible wrote:
>> 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))

>
> If aValue is signed, you need to remove the sign extension.
>
> bValue = ( (aValue << 8) | ( 0xff & (aValue >> 8) ))
>

Be aware that the constant 0xff can have its sign extended when it's
promoted to 16 bits. So

0xff & (aValue >> 8)

can become:

0xffff & (aValue >> 8)

which isn't what you intended and doesn't kill the sign extension.

I always write:

0x0ff & (aValue >> 8)

to avoid this problem.

-Dana

Dana Cartwright, Jun 14, 2005
7. ### PanjandrumGuest

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

Only use the predefined functions htonl, htons, ..., and not your own
hacks.

Panjandrum, Jun 14, 2005
8. ### red floydGuest

Panjandrum wrote:
> invincible wrote:
>
>>hi I wanted to convert Little Endian to big endian for 2 byte value

>
>
> Only use the predefined functions htonl, htons, ..., and not your own
> hacks.
>

Not part of the ISO standard. I think they're POSIX, but they aren't
guaranteed on all systems.

red floyd, Jun 14, 2005
9. ### Old WolfGuest

Dana Cartwright wrote:
> "Gianni Mariani" <> wrote:
> >
> > If aValue is signed, you need to remove the sign extension.
> >
> > bValue = ( (aValue << 8) | ( 0xff & (aValue >> 8) ))
> >

>
> Be aware that the constant 0xff can have its sign extended when
> it's promoted to 16 bits.

Actually it can't. 0xff is (int) 255, and this must remain
as 255 when it is promoted to anything. In fact it
does not get promoted at all in this example.

> I always write:
>
> 0x0ff & (aValue >> 8)
>
> to avoid this problem.

0x0ff is identical to 0xff .

Old Wolf, Jun 14, 2005
10. ### Old WolfGuest

Robbie Hatley wrote:
> "tuvok" wrote:
> >
> > Try this:
> > bValue = (aValue << 8) | (aValue >> 8);

>
> Hmmm... I think that would work if you make aValue and
> bValue unsigned. But if they're signed, that right shift
> is going to pour-in a bunch of 1s if the msb of aValue
> is a 1.

It's implementation-defined as to what gets poured into
those bits. (But 1-filling is very common.)

> #include <iostream>
> int main(void)
> {
> register signed int Blat = 0x80000000;

0x80000000 is an unsigned int (or long) that's outside
of the range of signed int. So you cause I-D behaviour
by assigning it to a signed int. (IIRC -- it might be undefined).

To get well-defined behaviour you could write:
int Blat = INT_MIN;

but that won't always get you the representation 0x80000000.

> Blat = Blat >> 21;
> std::cout << std::hex << Blat << std::endl;
> return 0;
> }
>
> Do you expect that to print 400?
> No, it prints fffffc00.