Note that there are some explanatory texts on larger screens.

plurals
  1. POParse four bytes to floating-point in C
    primarykey
    data
    text
    <p>How do I take four received data <em>bytes</em> and assemble them into a <em>floating-point</em> number?</p> <p>Right now I have the <em>bytes</em> stored in an array, which would be, <strong>received_data[1]</strong> ... <strong>received_data[4]</strong>. I would like to store these four <em>bytes</em> as a single 32-bit single precision <em>float</em>. </p> <p>-Thanks</p> <p>I'm actually receiving a packet with 19 bytes in it and assembling two sets of four bytes to make two floating-point numbers. So <strong>received_data[1]</strong> to <strong>received_data[4]</strong> is one float, and <strong>received_data[5]</strong> to <strong>received_data[8]</strong> is the other...</p> <p><strong>*UPDATE:**** **More Info...</strong><br> The first <em>bit</em>, or the <strong>sign</strong> bit of the <em>float</em>, is the seventh bit the the first <em>byte</em>, here's what I did to check... </p> <pre><code>#define CHECK_BIT(var,pos) ((var) &amp; (1&lt;&lt;(pos))) if( CHECK_BIT(received_data[1], 7)) //set or clear LED2 {LATAbits.LATA2=1;}else{LATAbits.LATA2=0;} if( CHECK_BIT(received_data[5], 7)) //set or clear LED3 {LATAbits.LATA3=1;}else{LATAbits.LATA3=0;} </code></pre> <p>I then checked this by sending alternating positive and negative numbers from my data source, which should have changed the LED's accordingly and it worked. </p> <p>As long as all that holds true, that means the bytes are in the right order, but they are stored little-endian, correct? Will this be stored into the float in the right order? I haven't had any luck parsing the bytes and reading it as a float yet...</p> <p><em>If anyone has any experience with it, I'm using the C18 MPLAB IDE compiler.</em> </p> <p><strong><em>FINAL UPDATE:</em></strong><br> I have my application running! Just some minor bug fixes now!</p> <p><strong><em>THANKS!</em></strong> </p> <p><em>Thanks for all the help guys! I'm new to stackoverflow, but this community on here is truly awesome! And a tool like this is truly priceless! I can not even begin to tell you how much time and frustration you have all saved me!</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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