title: Extract and Convert NSData from BLE to Float in Swift
body: <p>I'm developing an IOS App that handles a BlueTooth SensorTag.
  That SensorTag is based on the TI BLE SensorTag, but we had some of the Sensors removed.</p>
  
  <p>In the sourcecode of the original IOS App from TI the XYZ-Values are calculated like follows
  with KXTJ9_RANGE defined as 1.0 in my implementation and KXTJ9 is the Accelerometer built on the SensorTag</p>
  
  <pre><code>+(float) calcXValue:(NSData *)data {
    char scratchVal[data.length];
    [data getBytes:&amp;scratchVal length:3];
    return ((scratchVal[0] * 1.0) / (64 / KXTJ9_RANGE));
  }
  </code></pre>
  
  <p>The data comes as hexadecimal like "fe850d" and by the method will be cut in 3 parts.
  Now i'm trying to convert this method to swift, but i get the wrong numbers back</p>
  
  <p>e.g. <code>fe</code> should return something around <code>0.02</code> what the objective C Code does</p>
  
  <p>My Swift Code so far</p>
  
  <pre><code>class Sensor: NSObject {
    let Range: Float = 1.0
    var data: NSData
    var bytes: [Byte] = [0x00, 0x00, 0x00]
  
    init(data: NSData) {
      self.data = data
      data.getBytes(&amp;bytes, length: data.length)
    }
  
    func calcXValue()-&gt;Float {
      return ((Float(bytes[0]) * 1.0) / (64.0 / Range))
    }
  ...
  }
  </code></pre>
  
  <p>I believe problem must lie around my <code>Float(bytes[0])</code> because that makes <code>254</code> out of <code>fe</code> whereas <code>scratchVal[0]</code> in ObjectiveC is around 64</p>
  
  <p>But my main problem is, i was all new with IOS programming when i had to begin with this project, so i chose to use Swift to learn and code the app with it.</p>
  
  <p>Right now i use the original Objective C Code from TI to use with our SensorTag, but i would prefer using Swift for every part in the App.</p>