Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, there seems to be problems with what you are doing on two different levels. Part of the confusion here seems to stem for your use of pointers, what type of objects they point to, and then the interpretation of the encoding of the values in the memory pointed to by the pointer(s).</p> <p>The encoding of multi-byte entities in memory is what is referred to as endianess. The two common encodings are referred to as <strong><em>Little Endian</em></strong> (LE) and <strong>Big Endian</strong> (BE). With LE, a 16-bit quantity like a short is encoded least significant byte (LSB) first. Under BE, the most significant byte (MSB) is encoded first.</p> <p>By convention, network protocols normally encode things into what we call "network byte order" (NBO) which also happens to be the same as BE. If you are sending and receiving memory buffers on big endian platforms, then you will not run into conversion problems. However, your code would then be platform dependent on the BE convention. If you want to write portable code that works correctly on both LE and BE platforms, you should not assume the platform's endianess.</p> <p>Achieving endian portability is the purpose of routines like <strong><em>ntohs()</em></strong>, <strong><em>ntohl()</em></strong>, <strong><em>htons()</em></strong>, and <strong><em>htonl()</em></strong>. These functions/macros are defined on a given platform to do the necessary conversions at the sending and receiving ends:</p> <ul> <li><strong><em>htons()</em></strong> - Convert short value from host order to network order (for sending)</li> <li><strong><em>htonl()</em></strong> - Convert long value from host order to network order (for sending)</li> <li><strong><em>ntohs()</em></strong> - Convert short value from network order to host order (after receive)</li> <li><strong><em>ntohl()</em></strong> - Convert long value from network order to host order (after receive)</li> </ul> <p>Understand that your comment about accessing the memory when cast back to characters has no affect on the actual order of entities in memory. That is, if you access the buffer as a series of bytes, you will see the bytes in whatever order they were actually encoded into memory as, whether you have a BE or LE machine. So if you are looking at a NBO encoded buffer after receive, the MSB is going to be first - always. If you look at the output buffer after your have converted back to host order, if you have BE machine, the byte order will be unchanged. Conversely, on a LE machine, the bytes will all now be reversed in the converted buffer.</p> <p>Finally, in your conversion loop, the variable <code>total</code> refers to bytes. However, you are accessing the buffer as <code>shorts</code>. Your loop guard should not be <code>total</code>, but should be:</p> <p><code>total / sizeof( unsigned short )</code></p> <p>to account for the double byte nature of each <code>short</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload