使用Swift,我想将uint8_t数组中的字节转换为整数.
"C"示例:
char bytes[2] = {0x01, 0x02};
NSData *data = [NSData dataWithBytes:bytes length:2];
NSLog(@"data: %@", data); // data: <0102>
uint16_t value2 = *(uint16_t *)data.bytes;
NSLog(@"value2: %i", value2); // value2: 513
快速try :
let bytes:[UInt8] = [0x01, 0x02]
println("bytes: \(bytes)") // bytes: [1, 2]
let data = NSData(bytes: bytes, length: 2)
println("data: \(data)") // data: <0102>
let integer1 = *data.bytes // This fails
let integer2 = *data.bytes as UInt16 // This fails
let dataBytePointer = UnsafePointer<UInt16>(data.bytes)
let integer3 = dataBytePointer as UInt16 // This fails
let integer4 = *dataBytePointer as UInt16 // This fails
let integer5 = *dataBytePointer // This fails
在Swift中,从UInt8数组创建UInt16值的正确语法或代码是什么?
我对NSData版本感兴趣,正在寻找不使用临时数组的解决方案.