0

Im reading accelerometer data from an ibeacon that appears in the following string format:

x hex string value: "0160"
y hex string value: "ff14"
z hex string value: "0114"

Im expecting to see these values as double values ranging from 0g to 1g. How would you convert these hex strings into doubles in swift?

Dávid Pásztor
  • 45,571
  • 9
  • 73
  • 100
user869305
  • 141
  • 1
  • 7
  • 3
    Do you really get those strings, or is that the hexadecimal representation of binary data from the beacon? – Martin R Oct 31 '18 at 11:16
  • https://stackoverflow.com/questions/27189338/swift-native-functions-to-have-numbers-as-hex-strings ? – Larme Oct 31 '18 at 13:29

1 Answers1

3

Get the integer value from hex string with Int(_:radix:)

let string = "ff14"
let hexValue = Int(string, radix: 16)!

and divide by 65535 (16 bit) to get values between 0.0 and 1.0

let result = Double(hexValue) / 65535
Martin R
  • 510,973
  • 84
  • 1,183
  • 1,314
vadian
  • 253,546
  • 28
  • 306
  • 323