title: How to get float in bytes?
body: <p>I am using the <em>HIDAPI</em> to send some data to a USB device. This data can be sent only as <em>byte</em> array and I need to send some <code>float</code> numbers inside this data array. I know floats have <code>4</code> bytes. So I thought this might work:</p>
  
  <pre><code>float f = 0.6;
  char data[4];
  
  data[0] = (int) f &gt;&gt; 24;
  data[1] = (int) f &gt;&gt; 16;
  data[2] = (int) f &gt;&gt; 8;
  data[3] = (int) f;
  </code></pre>
  
  <p>And later all I had to do is:</p>
  
  <pre><code>g = (float)((data[0] &lt;&lt; 24) | (data[1] &lt;&lt; 16) | (data[2] &lt;&lt; 8) | (data[3]) );
  </code></pre>
  
  <p>But testing this shows me that the lines like <code>data[0] = (int) f &gt;&gt; 24;</code> returns always <code>0</code>. What is wrong with my code and how may I do this correctly (i.e. break a <code>float</code> inner data in <code>4 char</code> bytes and rebuild the same <code>float</code> later)? </p>
  
  <hr>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>I was able to accomplish this with the following codes:</p>
  
  <pre><code>float f = 0.1;
  unsigned char *pc;
  pc = (unsigned char*)&amp;f;
  
  // 0.6 in float
  pc[0] = 0x9A;
  pc[1] = 0x99;
  pc[2] = 0x19;
  pc[3] = 0x3F;
  
  std::cout &lt;&lt; f &lt;&lt; std::endl; // will print 0.6
  </code></pre>
  
  <p>and</p>
  
  <pre><code>*(unsigned int*)&amp;f = (0x3F &lt;&lt; 24) | (0x19 &lt;&lt; 16) | (0x99 &lt;&lt; 8) | (0x9A &lt;&lt; 0);
  </code></pre>
  
  <p>I know <code>memcpy()</code> is a "cleaner" way of doing it, but this way I think the performance is somewhat better.</p>