From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: Byte Order
Date: Wed, 12 Jan 2005 18:34:57 +0100 [thread overview]
Message-ID: <20050112173457.GS25737@lug-owl.de> (raw)
In-Reply-To: <20050112_170357_014648.r_zaca@ig.com.br>
[-- 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 --]
next prev parent reply other threads:[~2005-01-12 17:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-01-12 17:03 Byte Order r_zaca
2005-01-12 17:31 ` Eric Bambach
2005-01-12 17:34 ` Jan-Benedict Glaw [this message]
-- strict thread matches above, loose matches on Subject: below --
2005-01-13 10:05 r_zaca
2005-01-14 21:31 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050112173457.GS25737@lug-owl.de \
--to=jbglaw@lug-owl.de \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).