Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following functions pack/unpack bytes representing a single precision floating point value to/from a buffer in network byte order. Only the pack method needs to take endianness into account since the unpack method explicitly constructs the 32-bit value from the individual bytes by bit shifting them the appropriate amount and then OR-ing them together. These functions are only valid for C/C++ implementations that store a float in 32-bits. This is true for <a href="http://en.wikipedia.org/wiki/IEEE_754-1985" rel="nofollow noreferrer">IEEE 754-1985</a> floating point implementations.</p> <pre><code>// unpack method for retrieving data in network byte, // big endian, order (MSB first) // increments index i by the number of bytes unpacked // usage: // int i = 0; // float x = unpackFloat(&amp;buffer[i], &amp;i); // float y = unpackFloat(&amp;buffer[i], &amp;i); // float z = unpackFloat(&amp;buffer[i], &amp;i); float unpackFloat(const void *buf, int *i) { const unsigned char *b = (const unsigned char *)buf; uint32_t temp = 0; *i += 4; temp = ((b[0] &lt;&lt; 24) | (b[1] &lt;&lt; 16) | (b[2] &lt;&lt; 8) | b[3]); return *((float *) &amp;temp); } // pack method for storing data in network, // big endian, byte order (MSB first) // returns number of bytes packed // usage: // float x, y, z; // int i = 0; // i += packFloat(&amp;buffer[i], x); // i += packFloat(&amp;buffer[i], y); // i += packFloat(&amp;buffer[i], z); int packFloat(void *buf, float x) { unsigned char *b = (unsigned char *)buf; unsigned char *p = (unsigned char *) &amp;x; #if defined (_M_IX86) || (defined (CPU_FAMILY) &amp;&amp; (CPU_FAMILY == I80X86)) b[0] = p[3]; b[1] = p[2]; b[2] = p[1]; b[3] = p[0]; #else b[0] = p[0]; b[1] = p[1]; b[2] = p[2]; b[3] = p[3]; #endif return 4; } </code></pre>
 

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