Re: serialization class and endian



slebetman@xxxxxxxxx wrote:
toby wrote:
slebetman@xxxxxxxxx wrote:
toby wrote:
...
Well, again htonl() and htons() are completely redundant, since the
decomposition of an integer into bytes already explicitly defines an
ordering. I assumed you realised that these functions really only apply
if you are storing shorts/longs and then treating them as a binary
aggregate for I/O. Not useful for *byte-by-byte* processing of single
values.

See my other post. Docomposition of a 16 bit value 0x1a2b into bytes on
a big endian machines results in the byte stream: 0x1a, 0x2b while on a
little endian machine results in a byte stream 0x2b, 0x1a.
<snip>

You're missing a shift here.

Yes, sorry bout that. I meant:
bytes[0] = (number & 0xff00) >> 8;


But the OP is encoding as a string (assuming he meant binary not ASCII
- he seems confused on this question), so I alternatively proposed
above:
std::string str;
str += (char)(invalue >> 8); // use BigEndian ordering
str += (char)invalue;
Which is of course portable.

On little endian machines htons() will simply filp the
bytes around using the most optimal algorithm for the platform but on
big endian machines htons() typically gets optimised to nothing when
compiled (since the bytes are already in the right order). Using
htons() simply allows your code to work on big and little endian
machines without modification.

But both calls are, as I say above, only useful in the case you're
treating shorts and longs as aggregates - not if you are building an
encoding byte by byte.

Ah, C vs C++. I guess this is where the confusion between us lies. To
me, encoding byte by byte is:

unsigned char str[MAX_PACKET_LEN];
unsigned int i = 0;

invalue = htons(invalue);
memcpy(&str[i], &invalue, 2); i += 2;

Just to nitpick, this only works if the type of invalue is actually
'short' (and sizeof(short)==2, which it generally is).

This doesn't depend on the size of any types and really must be more
efficient, since it avoids the two function calls:
str[i++] = invalue>>8;
str[i++] = invalue;


if (i<MAX_PACKET_LEN) {
invalue2 = htons(invalue2);
memcpy(&str[i], &invalue2, 2); i += 2;
}

Of course the way I do it htons helps *a lot* while the way you do it
you don't need it.

.



Relevant Pages

  • Re: serialization class and endian
    ... Well, again htonland htons() are completely redundant, since the ... By "decomposition" I'm assuming you mean: ... you try to transmit the bytes between a big endian machine and a little ... // works on both big and little endian machines: ...
    (comp.programming)
  • Re: serialization class and endian
    ... Well, again htonland htons() are completely redundant, since the ... a big endian machines results in the byte stream: ... std::string str; ... encoding byte by byte is: ...
    (comp.programming)
  • Re: serialization class and endian
    ... Well, again htonland htons() are completely redundant, since the ... a big endian machines results in the byte stream: ... std::string str; ... encoding byte by byte is: ...
    (comp.programming)