title: Properly reading 4 bytes in -Hex- NSData form into its proper data type (float or int) in Swift
body: <p>I will try to be as explicit as possible I appreciate all the help in advance!.</p>
  
  <p>I am trying to write an iOS 8 app that uses the CoreBluetooth framework for Swift. I know how to initiate and obtain the values and subscribe to the values I want from the Bluetooth BLE peripherals in Swift, that will not be an issue. I do however need help in understanding and properly translating the values I'm receiving into the values that I can properly use.</p>
  
  <p>I am using a BLE peripheral to send analog data in a Float form. Checking for the characteristics.value.length property I know I receive 4 bytes in Hexadecimal form. Also using the LightBlue app, I also have understood that the format the information is being received is a 4-byte little endian float. </p>
  
  <p>An example of the values that I receive from the output window are:</p>
  
  <ul>
  <li>&lt;00404f44></li>
  <li>&lt;00404f44></li>
  <li>&lt;00804e44></li>
  <li>&lt;00404e44></li>
  <li>&lt;00c04d44></li>
  <li>&lt;00404d44></li>
  <li>&lt;00804c44></li>
  <li>&lt;00c04c44></li>
  <li>&lt;00404c44></li>
  </ul>
  
  <p>The analog values being sent are from a potentiometer ranging from 0 - 1023, and while a 0x0000803F hex gives me a value of 1.0000 and a 0x00C07F44 hex gives me a value of 1023.0000 (From the LightBlue app) I don't really know how to convert these values from Hex into Floats?</p>
  
  <p>I found a sample app that does this in Objective-C, however I'm new to swift and would like to do this app in swift-only. The Example app code that does this is the following:</p>
  
  <pre><code>float dataFloat(NSData *data)
  {
      uint8_t *p = (uint8_t*)[data bytes];
      NSUInteger len = [data length];
      return (sizeof(float) &lt;= len ? *(float*)p : 0);
  
  }    
  </code></pre>
  
  <p>Which is called by: </p>
  
  <pre><code>float analog = dataFloat(data);
  </code></pre>
  
  <p>I don't want to translate this information to a string however, because later I will try to do some playing around with the values (Once they have been translated into values I know and am comfortable with) But for now I would like my output window to display instead of a 0x00C07F44 hex for example, a 1023... </p>
  
  <p>I would also like to thank you all for taking the time to read my question, and while it may seem similar to other people's... I have tried their solutions and they don't seem to fit on mine, I admit I am quite new to this so I apologize and hope for your consideration. </p>
  
  <p>Please let me know if there's any other information I can provide that I may have missed!</p>
  
  <p>Sincerely, </p>
  
  <p>Maverik0106</p>