* Byte Order
@ 2005-01-12 17:03 r_zaca
2005-01-12 17:31 ` Eric Bambach
2005-01-12 17:34 ` Jan-Benedict Glaw
0 siblings, 2 replies; 5+ messages in thread
From: r_zaca @ 2005-01-12 17:03 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Mail message body --]
[-- Type: text/plain, Size: 290 bytes --]
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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Byte Order
2005-01-12 17:03 Byte Order r_zaca
@ 2005-01-12 17:31 ` Eric Bambach
2005-01-12 17:34 ` Jan-Benedict Glaw
1 sibling, 0 replies; 5+ messages in thread
From: Eric Bambach @ 2005-01-12 17:31 UTC (permalink / raw)
To: r_zaca; +Cc: linux-c-programming
On Wednesday 12 January 2005 11:03 am, you wrote:
> 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
> 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 majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Byte Order
2005-01-12 17:03 Byte Order r_zaca
2005-01-12 17:31 ` Eric Bambach
@ 2005-01-12 17:34 ` Jan-Benedict Glaw
1 sibling, 0 replies; 5+ messages in thread
From: Jan-Benedict Glaw @ 2005-01-12 17:34 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1877 bytes --]
On Wed, 2005-01-12 15:03:57 -0200, r_zaca <r_zaca@ig.com.br>
wrote in message <20050112_170357_014648.r_zaca@ig.com.br>:
Content-Description: Mail message body
> 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;
}
> 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
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +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));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Byte Order
@ 2005-01-13 10:05 r_zaca
2005-01-14 21:31 ` Glynn Clements
0 siblings, 1 reply; 5+ messages in thread
From: r_zaca @ 2005-01-13 10:05 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Mail message body --]
[-- Type: text/plain, Size: 1746 bytes --]
Thanks a lot, that is what I was looking for. A simple test that I can do,
using some C conde, to find out if the machine where the program is running
on uses Big or Little endian "memory organization".
On Wed, 2005-01-12 15:03:57 -0200, r_zaca <r_zaca@ig.com.br>
wrote in message <20050112_170357_014648.r_zaca@ig.com.br>:
Content-Description: Mail message body
> 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;
}
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Byte Order
2005-01-13 10:05 r_zaca
@ 2005-01-14 21:31 ` Glynn Clements
0 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2005-01-14 21:31 UTC (permalink / raw)
To: r_zaca; +Cc: linux-c-programming
r_zaca wrote:
> Thanks a lot, that is what I was looking for. A simple test that I can do,
> using some C conde, to find out if the machine where the program is running
> on uses Big or Little endian "memory organization".
#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
...
#elif __BYTE_ORDER == __BIG_ENDIAN
...
#elif __BYTE_ORDER == __PDP_ENDIAN
...
#endif
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-01-14 21:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-12 17:03 Byte Order r_zaca
2005-01-12 17:31 ` Eric Bambach
2005-01-12 17:34 ` Jan-Benedict Glaw
-- strict thread matches above, loose matches on Subject: below --
2005-01-13 10:05 r_zaca
2005-01-14 21:31 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).