converting a big endian to little endian

J

junky_fellow

Hi guys,

I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.

#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;

little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];

printf("big = 0x%x little = 0x%x\n",big,little);

}
 
L

loic-dev

Hello,
I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.

You can use 'shift' combined with 'and' operator to get the same
result:

/*
* convert big -> little endian and conversly
* this function assumes that sizeof(unsigned int) == 4
*/
unsigned int
endian_swap(unsigned int x)
{
return
(x>>24) |
((x>>8) & 0x0000ff00) |
((x<<8) & 0x00ff0000) |
(x<<24);
}

Cheers,
Loic.
 
D

David T. Ashley

Hi guys,

I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.

#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;

little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];

printf("big = 0x%x little = 0x%x\n",big,little);

}

The code above should work fine. However, the more traditional approach is
to use a union, i.e.

union
{
int i;
char c[4];
} pickapart;

pickapart.i = input_arg;
output0 = pickapart.c[0];

However, if you can find an approach involving shifting only, and if an
examination of the assembly-language shows that the compiler does it well
(probably without shifting), that would be the preferred approach, i.e.

output0 = input_arg & 0xFF;
output1 = (input_arg >>8) & 0xFF;
etc.

then that would be the preferred approach. The approach you cited can lead
to memory addressing problems if the int size is <32, and the approach I
cited with the union won't work for other sizes of integers. There should
be a more portable and general approach.
 
A

Anoop Saxena

Hi,
New here, so apologies if I have not followed any rules while
adding code snippets etc. Do let me know.
endian_swap(unsigned int x)
{
return
(x>>24) |
((x>>8) & 0x0000ff00) |
((x<<8) & 0x00ff0000) |
(x<<24);
}

Cheers,
Loic.

If int is 32 bits, then how good is the following code for unsigned
int's?

int main(void)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z = ( (x << 16) | ( x >> 16) );

return 0;
}
 
C

Chris Dollin

Anoop said:
If int is 32 bits, then how good is the following code for unsigned
int's?

int main(void)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z = ( (x << 16) | ( x >> 16) );

return 0;
}

Broken. It will generate 0x2211ffaa, not the required
0x1122aaff.

(It also has /definitely/ superfluous brackets and an
unnecessary initialisation of `z`, which are style issues,
and it would have been nice for there to be some kind
of output.)
 
K

Keith Thompson

I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.

#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;

little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];

printf("big = 0x%x little = 0x%x\n",big,little);

}

I'd use unsigned char rather than plain char. It's not likely to make
any real difference, but unsigned char is generally better for
arbitrary binary data.
 
S

Stephen Sprunk

Anoop Saxena said:
If int is 32 bits, then how good is the following code for unsigned
int's?

int main(void)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z = ( (x << 16) | ( x >> 16) );

return 0;
}

That converts little-endian to/from PDP-endian, not big-endian.

S
 
R

Random832

2006-12-14 said:
I don't doubt it, but those aren't the ones I was talking about.

Some people might think that without the ones you were talking about it
would have been the equivalent of (z=(x<<16))|(x>>16). The confusion is
that widespread.
 
A

Anoop Saxena

That converts little-endian to/from PDP-endian, not big-endian.

S


Absolutely my mistake. I got it mixed with my response to the following
thread in another C forum and mixed the posts. I apologize.
"
hi all.

Here is an interesting problem hope i will get a solution of it.

We all no the sizeof int depends on the hardware..Somewhere it is
2bytes and somewhere it is 4 bytes. Now i would like to present a
interesting problem based on this important property.

1)First you have to find out what is the size of the int in the
platform.
2) now suppose you got 2 bytes or 4 bytes... so in case of 2 bytes you
can represent a int no as int x = 0xa1a2; and in 4bytes you can
represent it as int x = a1a2a3a4; so now in case of 2 byte you have to
change the byte order i.i x = a1a2 should become x = a2a1 and in case
of 4bytes x = 0xa1a2a3a4 should become x = 0xa3a4a1a2.

hope you can provide with the solution. "
 
C

Chris Dollin

Random832 said:
Some people might think that without the ones you were talking about it
would have been the equivalent of (z=(x<<16))|(x>>16). The confusion is
that widespread.

I find that hard to believe, although if that's what you've seen,
that's what you've seen. (If "some" is small enough I'd put it
under "some people can misunderstand /anything/" ...)
 
R

Random832

2006-12-15 said:
I find that hard to believe, although if that's what you've seen,
that's what you've seen. (If "some" is small enough I'd put it
under "some people can misunderstand /anything/" ...)

In that case it would be not so much that bitwise operators are
confusing as it is that they're _intimidating_.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top