1

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
  • 51,403
  • 9
  • 85
  • 116
user869305
  • 151
  • 1
  • 8
  • 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

4

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
  • 529,903
  • 94
  • 1,240
  • 1,382
vadian
  • 274,689
  • 30
  • 353
  • 361