使用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
Swift Attempt:
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版本感兴趣,正在寻找一种不使用临时数组的解决方案。
答案 0 :(得分:40)
如果你想通过NSData
,那么它会像这样工作:
let bytes:[UInt8] = [0x01, 0x02]
println("bytes: \(bytes)") // bytes: [1, 2]
let data = NSData(bytes: bytes, length: 2)
print("data: \(data)") // data: <0102>
var u16 : UInt16 = 0 ; data.getBytes(&u16)
// Or:
let u16 = UnsafePointer<UInt16>(data.bytes).memory
println("u16: \(u16)") // u16: 513
可替换地:
let bytes:[UInt8] = [0x01, 0x02]
let u16 = UnsafePointer<UInt16>(bytes).memory
print("u16: \(u16)") // u16: 513
两种变体都假设字节是主机字节顺序。
更新Swift 3(Xcode 8):
let bytes: [UInt8] = [0x01, 0x02]
let u16 = UnsafePointer(bytes).withMemoryRebound(to: UInt16.self, capacity: 1) {
$0.pointee
}
print("u16: \(u16)") // u16: 513
答案 1 :(得分:5)
怎么样
let bytes:[UInt8] = [0x01, 0x02]
let result = (UInt16(bytes[1]) << 8) + UInt16(bytes[0])
使用循环,这很容易推广到更大的字节数组,并且可以将其包装在函数中以便于阅读:
let bytes:[UInt8] = [0x01, 0x02, 0x03, 0x04]
func bytesToUInt(byteArray: [UInt8]) -> UInt {
assert(byteArray.count <= 4)
var result: UInt = 0
for idx in 0..<(byteArray.count) {
let shiftAmount = UInt((byteArray.count) - idx - 1) * 8
result += UInt(byteArray[idx]) << shiftAmount
}
return result
}
println(bytesToUInt(bytes)) // result is 16909060
答案 2 :(得分:3)
我不知道swift的语法,但是如下所示:
let a:UInt16 = UInt16(bytes[0]) * 256 + UInt16(bytes[1])
答案 3 :(得分:3)
如果字节在NSData
对象中,您可以这样做(假设为data:NSData
):
var number: UInt16 = 0
data.getBytes(&number, length: sizeof(UInt16))
getBytes
方法在number
的内存位置最多写入两个字节(类似于C&#39; s memcpy
。
如果data
没有足够的字节,这不会导致您的应用崩溃。
(编辑:如果从缓冲区的开头开始,则无需使用范围)
答案 4 :(得分:2)
Martin R的答案非常棒,并且针对beta 6进行了很好的更新。
但是,如果您需要获取不在缓冲区开头的字节,则建议的withMemoryRebound
方法不提供重新绑定的范围。
我对此的解决方案,例如。从数组中选出第二个UInt16:
var val: UInt16 = 0
let buf = UnsafeMutableBufferPointer(start: &val, count: 1)
_ = dat.copyBytes(to: buf, from: Range(2...3))
答案 5 :(得分:2)
在Swift 3或更高版本中,您可以将字节[UInt8]
转换为Data
并使用UInt16
withUnsafeBytes { $0.pointee }
值
Swift 3或更高版本
let bytes: [UInt8] = [1, 2]
投射到UInt16
let uint16 = Data(bytes).withUnsafeBytes({$0.pointee}) as UInt16
或明确设置类型
let uint16: UInt16 = Data(bytes).withUnsafeBytes({$0.pointee})
为了消除冗长,我们可以创建一些扩展Collection
的计算属性并将元素约束为字节UInt8
:
extension Collection where Element == UInt8 {
var data: Data {
return Data(self)
}
var uint16: UInt16 {
precondition(count == 2, "Total number of bytes must be equal to two")
return data.withUnsafeBytes { $0.pointee }
}
}
let bytes: [UInt8] = [1, 2]
let uint16 = bytes.uint16
print(uint16) // 513
答案 6 :(得分:0)
假设小端编码。
要从[UInt8]转换为UInt16,您可以执行类似
的操作var x: [UInt8] = [0x01, 0x02]
var y: UInt16 = 0
y += UInt16(x[1]) << 0o10
y += UInt16(x[0]) << 0o00
为了转换为UInt32,此模式扩展到
var x: [UInt8] = [0x01, 0x02, 0x03, 0x04]
var y: UInt32 = 0
y += UInt32(x[3]) << 0o30
y += UInt32(x[2]) << 0o20
y += UInt32(x[1]) << 0o10
y += UInt32(x[0]) << 0o00
移位量的八进制表示可以很好地指示移位了多少个完整字节(8变为0o10,16变为0o20等)。
对于UInt16,这可以简化为以下内容:
var x: [UInt8] = [0x01, 0x02]
let y: UInt16 = reverse(x).reduce(UInt16(0)) {
$0 << 0o10 + UInt16($1)
}
以及UInt32的以下内容:
var x: [UInt8] = [0x01, 0x02, 0x03, 0x04]
let y: UInt32 = reverse(x).reduce(UInt32(0)) {
$0 << 0o10 + UInt32($1)
}
简化版本也适用于UInt64,并且还处理字节编码不使用所有字节的值,如[0x01,0x02,0x03]