1. ## endian conversion...

I wrote this program for endian conversion and in my current machine which is little endian it works fine. Is there some better logic to do the same or is this okay (i find it pretty confusing though :-)).

[insert]
Code:
```#include <stdio.h>
#include <stdlib.h>

void showbits(unsigned int i);

int main(void){

unsigned int i = 543295629, j, result = 0, temp,final = 0, k;
showbits(i);

for(j = 31; j > 0; j--){

temp = i;
k = temp >> j;
k = k & 1;
if (k == 1){

final = final;
result = result >> (31-j);
result = result | k;
result = result & 1;
result = result << (31-j);
final = final| result;
//showbits(result);

}
else{

final = final;
result = result >> (31-j);
result = result | k;
result = result & 1;
result = result << (31-j);
final = final| result;
//showbits(result);
}
}

// do it for the last bit as well

final = final;
temp = i;
temp = temp & 1;
result = result >> 31;
result = result | temp;
result = result & 1;
result = result << 31;
final = final| result;
//showbits(result);

printf("\n After conversion is done Big endian to Little endian we have");
showbits(final);

return 0;
}

void showbits(unsigned int i){

unsigned int u;
printf("\n");
for(u = (~0U >> 1) + 1; u > 0; u = u>>1){

putchar(i & u? '1' : '0');
}
putchar('\n');
}```

2. Code:
```#include <stdio.h>
#include <stdlib.h>

int end_swp(int *num)
{
int tmp, i;
char *in, *out;

in = (char *)num;
out = (char *)&tmp;
for (i = 3; i >= 0; i--){
out[i] = (*in++);
}
return tmp;
}

int main(void)
{
int num, converted;

num = 0x86753090;
converted = end_swp(&num);
printf("ORIG: %08x, SWAP: %08x.\n", num, converted);
return 0;
}```
Don't know if it is _better_, but it is shorter.

3. The bits don't get reversed. Just the bytes.

Code:
```unsigned int SwapEndian( unsigned int x )
{
return     ( ( x >> 24 ) & 0x000000FF )
|   ( ( x >>  8 ) & 0x0000FF00 )
|   ( ( x <<  8 ) & 0x00FF0000 )
|   ( ( x << 24 ) & 0xFF000000 );
}```

4. I just use the ntohl(), htonl(), ntohs(), htons() functions.
Is there a reason you can't use them?

5. Originally Posted by cpjust
I just use the ntohl(), htonl(), ntohs(), htons() functions.
Is there a reason you can't use them?
No just acquainting myself with the bit operations so thati feel a bit comfortable. Also i havent yet gone through these functions so i dont know what do these functions do.

6. This is the code I use to go from big to little. Simple, simple. It'll work the other way too.

Code:
```union  myunion {  // "myunion" is the type name.
char data[8] ;
int  myint ;
} ;

. . .

int fix_int(char * n, int len ) {
unsigned char temp ;
int i , j ;
myunion mydata ;

memcpy(mydata.data, n, len) ; // Copy data into my union

for (i = 0 , j = len-1 ; i < j ; ++i, --j ) {  // logic will work for any length value

// To get little endian frm Big endian, swap this: 0X01020304 into this: 0X04030201

temp = mydata.data[i] ;
mydata.data[i] = mydata.data[j] ;
mydata.data[j] = temp ;
}

return mydata.myint ;
}```

7. Originally Posted by cpjust
I just use the ntohl(), htonl(), ntohs(), htons() functions.
Is there a reason you can't use them?
On a big-endian platform those functions are no-ops

8. Originally Posted by Kennedy
Code:
```#include <stdio.h>
#include <stdlib.h>

int end_swp(int *num)
{
int tmp, i;
char *in, *out;

in = (char *)num;
out = (char *)&tmp;
for (i = 3; i >= 0; i--){
out[i] = (*in++);
}
return tmp;
}

int main(void)
{
int num, converted;

num = 0x86753090;
converted = end_swp(&num);
printf("ORIG: %08x, SWAP: %08x.\n", num, converted);
return 0;
}```
Don't know if it is _better_, but it is shorter.
Are you sure that this does the little endian to big endian conversion. Because it doesnt seem to do that .

9. Originally Posted by roaan
Are you sure that this does the little endian to big endian conversion. Because it doesnt seem to do that .
It gives (0x)90307586, which is the correct answer.

10. here is the result that I get:
Code:
`ORIG: 86753090, SWAP: 90307586.`
EDIT: Snot!! too slow on the mouse. . . stupid mouse.

11. Originally Posted by brewbuck
On a big-endian platform those functions are no-ops
Which is correct if you're planning to send stuff over a network.

12. Originally Posted by roaan
No just acquainting myself with the bit operations so thati feel a bit comfortable. Also i havent yet gone through these functions so i dont know what do these functions do.
IMO that's good roaan, I have a tendency to write my own versions of standard library functions too, mostly in a quest for understanding, as you say.

However, when you are writing "production" grade software you should probably stick to those standard function when available, since they carry a bit more of a "playtested" guarantee and may save you unforeseen maintenance headaches, altho, as brewbuck is about to point out, the ntoh and hton functions maybe have a slightly more specific purpose and are for unsigned types.

13. Originally Posted by cpjust
Which is correct if you're planning to send stuff over a network.
Yes, my point was that they are not general endian conversion functions which is what you seemed to be implying.