我想将UInt32
添加到我使用[UInt8]
的字节缓冲区。在java中,有一个方便的ByteBuffer类,它具有像putInt()这样的方法,就像这样。怎么可以在swift中完成?
我想我可以解决这个问题如下:
let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15
var byteArray = [UInt8](count: 4, repeatedValue: 0)
for i in 0...3 {
byteArray[i] = UInt8(0x0000FF & example >> UInt32((3 - i) * 8))
}
这是非常冗长的,任何更简单的方法?
答案 0 :(得分:13)
你的循环可以更紧凑地写成
let byteArray = 24.stride(through: 0, by: -8).map {
UInt8(truncatingBitPattern: example >> UInt32($0))
}
或者,创建一个UnsafeBufferPointer
并转换它
到一个数组:
let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15
var bigEndian = example.bigEndian
let bytePtr = withUnsafePointer(&bigEndian) {
UnsafeBufferPointer<UInt8>(start: UnsafePointer($0), count: sizeofValue(bigEndian))
}
let byteArray = Array(bytePtr)
print(byteArray) // [72, 66, 1, 15]
Swift 3更新(Xcode 8 beta 6):
var bigEndian = example.bigEndian
let count = MemoryLayout<UInt32>.size
let bytePtr = withUnsafePointer(to: &bigEndian) {
$0.withMemoryRebound(to: UInt8.self, capacity: count) {
UnsafeBufferPointer(start: $0, count: count)
}
}
let byteArray = Array(bytePtr)
答案 1 :(得分:4)
改进了@Martin R的回答。使用UInt16,UInt32和UInt64:
protocol UIntToBytesConvertable {
var toBytes: [Byte] { get }
}
extension UIntToBytesConvertable {
func toByteArr<T: Integer>(endian: T, count: Int) -> [Byte] {
var _endian = endian
let bytePtr = withUnsafePointer(to: &_endian) {
$0.withMemoryRebound(to: Byte.self, capacity: count) {
UnsafeBufferPointer(start: $0, count: count)
}
}
return [Byte](bytePtr)
}
}
extension UInt16: UIntToBytesConvertable {
var toBytes: [Byte] {
return toByteArr(endian: self.littleEndian,
count: MemoryLayout<UInt16>.size)
}
}
extension UInt32: UIntToBytesConvertable {
var toBytes: [Byte] {
return toByteArr(endian: self.littleEndian,
count: MemoryLayout<UInt32>.size)
}
}
extension UInt64: UIntToBytesConvertable {
var toBytes: [Byte] {
return toByteArr(endian: self.littleEndian,
count: MemoryLayout<UInt64>.size)
}
}
答案 2 :(得分:2)
我也有类似的需求,并试图学习一些有关手动内存管理的知识。我是否缺少某些东西,或者使用较新版本的Swift变得更容易了吗? (我正在使用Swift 5)
从[UInt8]
创建字节数组(UInt32
)
let example: UInt32 = 1
let byteArray = withUnsafeBytes(of: example.bigEndian) {
Array($0)
}
print(byteArray) // [0, 0, 0, 1]
或者如果要附加到现有数组,则可以在闭包中完成:
var existingArray: [UInt8] = [1, 2, 3]
let example: UInt32 = 1
withUnsafeBytes(of: example.bigEndian) {
existingArray.append(contentsOf: $0)
}
print(existingArray) // [1, 2, 3, 0, 0, 0, 1]
答案 3 :(得分:0)
我对Xcode 8 Beta 6有类似的问题: 写这一行
var value = String(cString: sqlite3_column_text(stmt, index))
to
let txt = UnsafePointer<Int8>(sqlite3_column_text(stmt, index))
并解决问题
答案 4 :(得分:0)
改进了@Martin R的回答。
func toByteArrary<T>(value: T) -> [UInt8] where T: UnsignedInteger, T: FixedWidthInteger{
var bigEndian = value.bigEndian
let count = MemoryLayout<T>.size
let bytePtr = withUnsafePointer(to: &bigEndian) {
$0.withMemoryRebound(to: UInt8.self, capacity: count) {
UnsafeBufferPointer(start: $0, count: count)
}
}
return Array(bytePtr)
}
答案 5 :(得分:0)
另一种选择是直接扩展FixedWidthInteger
协议,因此任何UnsignedInteger
都会自动免费获得该功能。这就是@Benson答案的样子:
extension FixedWidthInteger where Self: UnsignedInteger {
var bytes: [UInt8] {
var _endian = littleEndian
let bytePtr = withUnsafePointer(to: &_endian) {
$0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<Self>.size) {
UnsafeBufferPointer(start: $0, count: MemoryLayout<Self>.size)
}
}
return [UInt8](bytePtr)
}
}
我们可以通过单元测试来验证这一点:
func test_bytes() {
XCTAssertEqual(UInt8.min.bytes, [0])
XCTAssertEqual(UInt8.max.bytes, [255])
XCTAssertEqual(UInt16.min.bytes, [0, 0])
XCTAssertEqual(UInt16.max.bytes, [255, 255])
XCTAssertEqual(UInt32.min.bytes, [0, 0, 0, 0])
XCTAssertEqual(UInt32.max.bytes, [255, 255, 255, 255])
XCTAssertEqual(UInt64.min.bytes, [0, 0, 0, 0, 0, 0, 0, 0])
XCTAssertEqual(UInt64.max.bytes, [255, 255, 255, 255, 255, 255, 255, 255])
}