In my application I am trying to convert Data to Int 16 but I must be doing something wrong. Here is an example of my problem
let x = "0000001110000111"
if let number = Int(x, radix: 2) {
print(number) // This give 903 which is what I would expect
}
let y = Data(x.utf8)
let convertedData = y.withUnsafeBytes {pointer in
return pointer.load(as: Int16.self)
}
print (convertedData) // This gives 12336 which is not what I was expecting
let str = String(decoding: y, as: UTF8.self)
print (str) // I wanted to check and make sure I got the correct binary back
// This gives 0000001110000111 as I would expect
What am I doing wrong here?