Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Bytes' orde...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 17 of 30 Topic 9591 of 9971
Post > Topic >>

Re: Bytes' order with different C++ compilers

by nickf3 <nickf3@[EMAIL PROTECTED] > May 19, 2008 at 11:59 AM

On May 18, 1:21 am, Martin Bonner <martinfro...@[EMAIL PROTECTED]
> wrote:
> On May 17, 9:29 am, nickf3 <nic...@[EMAIL PROTECTED]
> wrote:
>
> > On May 16, 8:27 pm, Alan McKenney <alan_mckenn...@[EMAIL PROTECTED]
> wrote:
....
>
> > > A.   // only works if host is big-endian
>
> > >       unsigned char *buf = ....
> > >       unsigned long uint32value;
>
> > >       memcpy( &uint32value, buf+ipos, 4 );
>
> > > B.   // works regardles of endianness of your host
>
> > >       uint32value = ( buf[ipos]   << 24 ) |
> > >                     ( buf[ipos+1] << 16 ) |
> > >                     ( buf[ipos+2] <<  8 ) |
> > >                       buf[ipos+3] ;
>
> > > I generally use B, to avoid having to worry
> > > about endianness.
>
> > Please don't reinvent the wheel, use standard library:
>
> >       #include <arpa/inet.h>
>
> >       uint32_t
> >       htonl(uint32_t hostlong);
>
> >       uint16_t
> >       htons(uint16_t hostshort);
>
> >       uint32_t
> >       ntohl(uint32_t netlong);
>
> >       uint16_t
> >       ntohs(uint16_t netshort);
>
> > These are noops on big-endian machines, so there's no overhead.
>
> Except that they are /not/ part of the standard library unless you can
> assume Posix.  Also they have a horrible interface.  The code Alan
> wrote took a buffer of unsigned chars (presumably containing octets
> from the communications protocol) and converted it to a long in a way
> that will work on any compiler that implements the C++ standard (and a
> fair few that don't). ntohl et-al assumes all sorts of things about
> the representations (which may not be true, eg, on a DSP)
>


Let's look at the (open) source, shall we:

/*      $OpenBSD: ntohl.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
/*
 * Written by J.T. Conklin <jtc@[EMAIL PROTECTED]
>.
 * Public domain.
 */

#include <sys/types.h>
#include <machine/endian.h>

#undef ntohl

u_int32_t
ntohl(u_int32_t x)
{
#if BYTE_ORDER == LITTLE_ENDIAN
        u_char *s = (u_char *)&x;
        return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 |
s[3]);
#else
        return x;
#endif
}

Taken directly from OpenBSD cvs. Looks familiar, doesn't it?
Where's that horror you speak of?
Then, there's arch-specific code for i386:

.... BSD license

/* hostorder = ntohl(netorder) */
ENTRY(ntohl)
        movl        4(%esp),%eax
        rorw        $8,%ax
        roll        $16,%eax
        rorw        $8,%ax
        ret

Then, sparc:

.... BSD license

/* hostorder = ntohl(netorder) */
ENTRY(ntohl)
        retl
        nop

And many other archs.


It's perfectly fine (I'd say, required) to _understand_
how to write ****table code, but would you want every
programmer on your team doing this stuff by hand, or
would you want them _reusing_ the existing libs?

Yes, it's not a part of the C++ std lib, but neither is that
socket API you use to get to the "communication channel", nor
whatever API you use to talk to a DSP (which doesn't have
C interface, only C++, right?)

Unless you elaborate on that last statement in
your post, I call it FUD.

--
 Nikolai

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 30 Posts in Topic:
Bytes' order with different C++ compilers
Ulysses4ever@[EMAIL PROTE  2008-05-11 16:40:44 
Re: Bytes' order with different C++ compilers
Carl Barron <cbarron41  2008-05-11 21:59:54 
Re: Bytes' order with different C++ compilers
Francis Glassborow <fr  2008-05-13 10:56:10 
Re: Bytes' order with different C++ compilers
Tony Delroy <tony_in_d  2008-05-13 21:14:12 
Re: Bytes' order with different C++ compilers
ThosRTanner <ttanner2@  2008-05-14 15:13:31 
Re: Bytes' order with different C++ compilers
Ulysses4ever@[EMAIL PROTE  2008-05-14 15:13:17 
Re: Bytes' order with different C++ compilers
Francis Glassborow <fr  2008-05-14 18:10:42 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-15 11:17:54 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-15 16:04:27 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-16 17:48:11 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-16 18:27:29 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-17 02:29:20 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-17 14:35:47 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-17 23:21:56 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-17 23:21:45 
Re: Bytes' order with different C++ compilers
Greg Herlihy <greghe@[  2008-05-19 06:05:51 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-19 11:59:02 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-19 12:00:38 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-20 05:14:57 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-21 01:22:18 
Re: Bytes' order with different C++ compilers
"C. M. Heard" &  2008-05-21 01:28:17 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-21 14:03:42 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-21 14:01:51 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-21 14:27:32 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-22 01:14:57 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-22 14:58:37 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-24 09:28:08 
Re: Bytes' order with different C++ compilers
Gerhard Fiedler <gelis  2008-05-25 09:57:35 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-26 16:16:35 
Re: Bytes' order with different C++ compilers
Gerhard Fiedler <gelis  2008-05-28 10:41:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:48:46 CDT 2008.