NSData从蓝牙到整数(NSData from bluetooth to integer)
我每隔2秒从蓝牙获取数据,
func device(device: JDevice!, didUpdateData data: NSData!, type typeCode: Int8, error: NSError!) { print(data) }打印结果是:
<6d000000> <6e000000> <6f000000> <70000000> <71000000> <72000000>如何将此数据转换为整数或字符串?
编辑 -------------------------
let des = data.description as NSString let dataAsString = des.substringWithRange(NSMakeRange(1, data.description.characters.count-2)) var value = UInt8(0) data.getBytes(&value, range: NSMakeRange(0, dataAsString.characters.count/2)) print(value)我找到了解决方案,但我不清楚为什么我使用dataAsString.characters.count作为data.getBytes函数中范围的长度。
我也有这个错误:
'NSRangeException', reason: '*** -[_NSInlineData getBytes:range:]: range {0, 8} exceeds data length 4' *** First throw call stack:这个错误是什么意思? 谢谢!
I get data from bluetooth every 2 seconds,
func device(device: JDevice!, didUpdateData data: NSData!, type typeCode: Int8, error: NSError!) { print(data) }The print result is:
<6d000000> <6e000000> <6f000000> <70000000> <71000000> <72000000>How can I convert this data to an integer or string?
EDIT -------------------------
let des = data.description as NSString let dataAsString = des.substringWithRange(NSMakeRange(1, data.description.characters.count-2)) var value = UInt8(0) data.getBytes(&value, range: NSMakeRange(0, dataAsString.characters.count/2)) print(value)I found the solution, but I am not very clear on why I use dataAsString.characters.count as the length of the range in the data.getBytes function.
I also got this error:
'NSRangeException', reason: '*** -[_NSInlineData getBytes:range:]: range {0, 8} exceeds data length 4' *** First throw call stack:What does this error mean? Thanks!
最满意答案
let des = data.description as NSString let dataAsString = des.substringWithRange(NSMakeRange(1, data.description.characters.count-2)) var value = UInt8(0) data.getBytes(&value, range: NSMakeRange(0, dataAsString.characters.count/2)) print(value) let des = data.description as NSString let dataAsString = des.substringWithRange(NSMakeRange(1, data.description.characters.count-2)) var value = UInt8(0) data.getBytes(&value, range: NSMakeRange(0, dataAsString.characters.count/2)) print(value)更多推荐
发布评论