K
Keith Thompson
Yes, it is "htonl()." Thank you very much. It was my mistake.
Are there any equivalent functions in the standard C library?
Not really.
Is there even the concept of byte ordering in C?
Well, sort of. Any C object can be viewed as an array of bytes
(unsigned char), but the standard doesn't say much about what those
bytes are going to look like. For example:
#include <stdio.h>
int main(void)
{
unsigned int x = 0x12345678;
unsigned char *ptr = (unsigned char*)&x;
int i;
printf("x = 0x%x\n", x);
for (i = 0; i < sizeof x; i ++) {
printf(" 0x%x", (unsigned int)ptr);
}
printf("\n");
return 0;
}
On one platform I just tried, the output is:
x = 0x12345678
0x12 0x34 0x56 0x78
On another, it's:
x = 0x12345678
0x78 0x56 0x34 0x12
but a valid implementation could produce:
x = 0x12345678
0x23 0x12 0x56 0x78
or any of a number of other possibilities.
But don't be afraid to use system-specific functions if they're
appropriate. If you need to use something like htonl(), chances are
your program is already doing plenty of stuff that's not directly
supported by the standard C library (htonl() is mostly used in
networking, and all networking support is system-specific). (Just
don't expect detailed advice about it in comp.lang.c.)