ios - Extract and Convert NSData from BLE to Float in Swift -
ios - Extract and Convert NSData from BLE to Float in Swift -
i'm developing ios app handles bluetooth sensortag. sensortag based on ti ble sensortag, had of sensors removed.
in sourcecode of original ios app ti xyz-values calculated follows kxtj9_range defined 1.0 in implementation , kxtj9 accelerometer built on sensortag
+(float) calcxvalue:(nsdata *)data { char scratchval[data.length]; [data getbytes:&scratchval length:3]; homecoming ((scratchval[0] * 1.0) / (64 / kxtj9_range)); }
the info comes hexadecimal "fe850d" , method cutting in 3 parts. i'm trying convert method swift, wrong numbers back
e.g. fe
should homecoming around 0.02
objective c code does
my swift code far
class sensor: nsobject { allow range: float = 1.0 var data: nsdata var bytes: [byte] = [0x00, 0x00, 0x00] init(data: nsdata) { self.data = info data.getbytes(&bytes, length: data.length) } func calcxvalue()->float { homecoming ((float(bytes[0]) * 1.0) / (64.0 / range)) } ... }
i believe problem must lie around float(bytes[0])
because makes 254
out of fe
whereas scratchval[0]
in objectivec around 64
but main problem is, new ios programming when had begin project, chose utilize swift larn , code app it.
right utilize original objective c code ti utilize our sensortag, prefer using swift every part in app.
on current ios , os x platforms, char
signed quantity, input fe
treated negative number.
byte
on other hand alias uint8
unsigned. utilize [int8]
array instead same behaviour in objective-c code.
ios swift floating-point type-conversion nsdata
Comments
Post a Comment