Skip to contentSkip to conversation

Byte Order

DISCUSSION r_zaca 3 participants 3 messages Jan 12, 2005

lore
Share & tools
lore

r_zaca

author
#0 lore raw

Hello all,

How can I say if the machine where I am working uses "Host Byte Order" or
"Network Byte Order"?
Is it an obvius question? I mean, all hosts (machines) uses "Host Byte
Order" and when it needs to send data through the network it uses "Network Byte Order".
Thanks.

#1 lore raw
Quoted 8 lines · you

Hello all,

How can I say if the machine where I am working uses "Host Byte Order" or
"Network Byte Order"?
Is it an obvius question? I mean, all hosts (machines) uses "Host Byte
Order" and when it needs to send data through the network it uses "Network Byte Order".
Thanks.

Well,

Im not sure if this answers your question, but when doing network programming
you should always use hton and ntoh conversions to make your program portable whether or not your machine uses the same byte order as the network.

If you;re asking which architectures are Big Endian and little endian and how
to tell..then im afraid I dont know. Google will tell you which architectures are which.

However, man pages also say "On machines which have a byte order which is the same as the network order, routines are defined as null macros." so its safe and desirable to use whether your machine uses the same of different ordering anyways.

P.S. If you want to check Endianess perhaps you can pass an int through htons or ntohs (etc.) and see if it changes the values. If it doesn't, then you're using the same ordering as the network. Perhaps someone can clarify if this will actually work its just a stab in the dark.
----------------------------------------
--EB

Quoted 4 lines

All is fine except that I can reliably "oops" it simply by trying to read from /proc/apm (e.g. cat /proc/apm).
oops output and ksymoops-2.3.4 output is attached.
Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and a ballistic missile.

                --Alan Cox LKML-December 08,2000

----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to r_zaca

On Wed, 2005-01-12 15:03:57 -0200, r_zaca <[email protected]> wrote in message <[email protected]>:
Content-Description: Mail message body

Quoted 2 lines

How can I say if the machine where I am working uses "Host Byte Order" or
"Network Byte Order"?

Something like this should do the job:

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])

{
        uint32_t i = 0x44332211;
        unsigned char *c;

        c = (unsigned char *) &i;

        if (c[0] == 0x11 && c[1] == 0x22 && c[2] == 0x33 && c[3] == 0x44)
                printf ("This is a little-endian host\n");
        else if (c[0] == 0x44 && c[1] == 0x33 && c[2] == 0x22 && c[3] == 0x11)
                printf ("This is a big-endian host\n");
        else if (c[0] == 0x33 && c[1] == 0x44 && c[2] == 0x11 && c[3] == 0x22)
                printf ("This is a pdp-endian host\n");
        else {
                printf ("This host is broken:-)\n");
                return EXIT_FAILURE;
        }

        return EXIT_SUCCESS;
}
Quoted 3 lines

Is it an obvius question? I mean, all hosts (machines) uses "Host Byte
Order" and when it needs to send data through the network it uses "Network Byte Order".

The rule is easy: Just *always* either use htonl/htons/ntohl/ntohs or transmit the number in readable format. The nice thing is that any sane libc will implement those as macros, so if your host already works in network byte order, the conversion will get optimized away.

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));