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 24 of 30 Topic 9591 of 9984
Post > Topic >>

Re: Bytes' order with different C++ compilers

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > May 21, 2008 at 02:27 PM

nickf3 wrote:

> ...
>>
>> The problem is that sane communications libraries deal in sequences
>> of octets stored in unsigned char (which are then packed up in the
>> correct order).  ntohl is based on an approach where you store bytes
>> into the memory of a struct, and then fix-up the byte order.  It
>> doesn't work if your platform has CHAR_BIT == 64; it requires you to
>> control padding between member variables of a struct; it requires you
>> to declare the structure with specific-sized members (rather than a
>> natural "int" or "long").
>>
>> Just Say No.
> 
> No :)
> 
> So how this is going to work then?
> 
>        uint32value = ( buf[ipos]   << 24 ) |
>                      ( buf[ipos+1] << 16 ) |
>                      ( buf[ipos+2] <<  8 ) |
>                        buf[ipos+3] ;
> 
> With CHAR_BIT = 32 and sizeof(u_int32_t) = 1 (buf + ipos)
> and (buf + ipos + 1) will point 32 bits apart, not 8, right?

Yes, that is correct.
And probably, each element of buf will have a value in the range [0,255]
(or possibly [-128,127]).

For reference, I am currently working on a platform with CHAR_BIT == 16.
When I do serial communications (octet oriented), each incoming octet
gets stored in its own (16-bit wide) byte.
If I want to reconstruct a 16-bit or 32-bit value, I need to use the
****fting method shown above.

> Or your 32 bit chars overlap?
> And just for giggles, with CHAR_BIT = 64, is sizeof(u_int32_t) == 1/2?

That depends on how you round-off the result of the division. :-)
Either you round up, and you have a type u_int32_t that is actually 64
bits wide, or you round down and you have a zero-width type that can't
exist.

> 
> --
>   Nikolai
> 
Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/

      [ 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 Sun Sep 7 4:18:48 CDT 2008.